Home > Net >  Django - User Password Change by coding
Django - User Password Change by coding

Time:07-19

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

CodePudding user response:

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
  • Related