Home > Net >  Django change password
Django change password

Time:01-17

Trying to change password using PasswordChangeView, but cannot get it working.

urls.py

from django.contrib.auth import views as auth_views

urlpatterns = [
    path('profiles/settings/', update_profile, name='update_profile'),
    path('profiles/settings/', auth_views.PasswordChangeView.as_view(template_name='accounts/settings.html'), 
        name='password_change'),
]

And i am trying to get the input fields correct in my html

            <div  role="tabpanel" id="password">
                <form id="id_password_change_form" method="POST" >{% csrf_token %}
                  <div >
                    <label >Current Password</label>
                    <div >
                      <input 
                      type="password" 
                      placeholder="Enter your current password" 
                      name="old_password" 
                       
                      id="id_old_password" 
                      required="true" />
                    </div>
                  </div>
                  <div >
                    <label >New Password</label>
                    <div >
                      <input 
                      type="password" 
                      placeholder="Enter a new password" 
                      name="new_password1" 
                       
                      id="id_new_password1" 
                      required="true" />
                      <small>Password must be at least 8 characters long</small>
                    </div>
                  </div>
                  <div >
                    <label >Confirm Password</label>
                    <div >
                      <input 
                      type="password" 
                      placeholder="Confirm your new password" 
                      name="new_password2" 
                       
                      id="id_new_password2" 
                      required="true" />
                    </div>
                  </div>
                  {% for field in form %}
                    {% for error in field.errors %}
                      <p style="color: red">{{ error }}</p>
                    {% endfor %}
                  {% endfor %}
                  <div >
                    <button type="submit" >Change Password</button>
                  </div>
                </form>

There is no error, and it do not update the password as supposed to. according to PasswordChangeView, I should not need to alter anything.

CodePudding user response:

I think you have a problem because of the wrong URLs definition, take a look at you urls.py:

from django.contrib.auth import views as auth_views

urlpatterns = [
    path('profiles/settings/', update_profile, name='update_profile'),
    path('profiles/settings/', auth_views.PasswordChangeView.as_view(template_name='accounts/settings.html'), 
        name='password_change'),
]

You always hit update_profile view instead of PasswordChangeView. I think this is typo.

  •  Tags:  
  • Related