修改密码
1. 修改密码后端逻辑
提示:
- 修改密码前需要校验原始密码是否正确,以校验修改密码的用户身份。
- 如果原始密码正确,再将新的密码赋值给用户。
class ChangePasswordView(LoginRequiredMixin, View):
"""修改密码"""
def get(self, request):
"""展示修改密码界面"""
return render(request, 'user_center_pass.html')
def post(self, request):
"""实现修改密码逻辑"""
# 接收参数
old_password = request.POST.get('old_password')
new_password = request.POST.get('new_password')
new_password2 = request.POST.get('new_password2')
# 校验参数
if not all([old_password, new_password, new_password2]):
return http.HttpResponseForbidden('缺少必传参数')
try:
request.user.check_password(old_password)
except Exception as e:
logger.error(e)
return render(request, 'user_center_pass.html', {'origin_pwd_errmsg':'原始密码错误'})
if not re.match(r'^[0-9A-Za-z]{8,20}$', new_password):
return http.HttpResponseForbidden('密码最少8位,最长20位')
if new_password != new_password2:
return http.HttpResponseForbidden('两次输入的密码不一致')
# 修改密码
try:
request.user.set_password(new_password)
request.user.save()
except Exception as e:
logger.error(e)
return render(request, 'user_center_pass.html', {'change_pwd_errmsg': '修改密码失败'})
# 清理状态保持信息
logout(request)
response = redirect(reverse('users:login'))
response.delete_cookie('username')
# # 响应密码修改结果:重定向到登录界面
return response
urls.py
# 修改密码
url(r'^pass/$', views.ChangePasswordView.as_view(), name='pass'),