1

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')
0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! its working now.. I have doubt. if user is trying to set weak password, will it accept it while change password?
set_password will not check the strength of the password - there is a built-in function you can call to check the passwords strength docs.djangoproject.com/en/4.0/topics/auth/passwords/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.