Justin
Justin

Reputation: 1479

PasswordChangeView not redirecting to PasswordChangeDoneView (or changing the password)

I am trying to use the Django authentication views. My login/logout views are working without a problem.

urls.py:

from django.urls import path
from django.contrib.auth import views as auth_views
from account import views

urlpatterns = [
    path('', views.dashboard, name = 'dashboard'),
    path('login/', auth_views.LoginView.as_view(), name = 'login'),
    path('logout/', auth_views.LogoutView.as_view(), name = 'logout'),
    path('password_change', auth_views.PasswordChangeView.as_view(), name = 'password_change'),
    path('password_change/done', auth_views.PasswordChangeDoneView.as_view(), name = 'password_change_done'),

]

password_change_form.html:

{% block body %}
  <h1>Change your password</h1>

    <p>Please use the following form to change your password:</p>
    <div class = 'password-change-form'>
      <form class="" action="{% url 'login' %}" method="post">
        {{ form.as_p }}
        {% csrf_token %}
        <input type="submit" name="" value="Change">
      </form>
    </div>

{% endblock %}

password_change_done.html:

{% block body %}
  <h1>Password Changed Successfully</h1>

{% endblock %}

I can navigate to /password_change and see the form, but when I fill it in and submit it in I am redirected to /login (I have made sure I was already logged in) and the password does not change. Does anyone know what the issue is?

Upvotes: 2

Views: 1236

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

This is because you specified {% url 'login' %} for the action="…" parameter, you need to change it with:

{% block body %}
  <h1>Change your password</h1>

    <p>Please use the following form to change your password:</p>
    <div class = 'password-change-form'>
      <form class="" action="{% url 'password_change' %}" method="post">
        {{ form.as_p }}
        {% csrf_token %}
        <input type="submit" name="" value="Change">
      </form>
    </div>

{% endblock %}

Upvotes: 2

Related Questions