我正在尝试允许更改登录用户的密码。编码如下…结果显示密码已更改,但密码未设置。
注意:模板有三个文本框
- "往往比;查看当前密码
- "password"查看新密码
- "confirm",以确认新密码。
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')
设置密码后需要对用户调用save()
,set_password()
不会将新密码保存到DB
u = request.user
u.set_password(request.POST['password'])
u.save() # Add this line