Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django – User Password Change by coding

I am trying to give permission to change password for logged in user. coded as below.. result comes as password has been changed but password not set.

Note: template having three text box

  1. "old" for current password
  2. "password" for new password
  3. "confirm" for confirmation of new password.
def changepassword(request):
    if request.method == 'POST':
        user = authenticate(request, username=request.user,password=request.POST['old'])
        if user is None:
            return render(request, 'pmp/changepassword.html', {'error': 'Current Password Enter Mismatch! '})
        else:
            try:
                if request.POST['password'] == request.POST['confirm']:
                    u = request.user
                    u.set_password('password')
                    return render(request, 'pmp/changepassword.html', {'success':'Password has been changed!'})
                else:
                    return render(request, 'pmp/changepassword.html',{'form': AuthenticationForm(), 'error': 'New Password and confirm Password mismatch!'})    
            except ValueError:
                return render(request, 'pmp/changepassword.html',{'form': AuthenticationForm(), 'error': 'Password not changed!'})
            
    return render(request, 'pmp/changepassword.html')

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to call save() on the user after setting the password, set_password() does not save the new password to the DB

    u = request.user
    u.set_password(request.POST['password'])
    u.save() # Add this line
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading