忘记密码处理流程
直接上代码
class ActiveView(View): # 主要功能是修改user_profile里的is_active字段为1
def get(self, request, active_code):
all_reocrds = EmailVerifyRecord.objects.filter(code=active_code)
if all_reocrds:
for record in all_reocrds:
email = record.email
user = UserProfile.objects.get(email=email)
user.is_active = True
user.save()
else: # 如果激活链接不存在,则返回激活失败
return render(request, "active_faild.html")
return render(request, 'login.html')
class ForgetPwdView(View):
def get(self, request):
forget_form = ForgetPwdForm() # 返回验证码form
return render(request, 'forget_pwd.html', {'forget_form': forget_form})
def post(self, request):
forget_form = ForgetPwdForm(request.POST)
if forget_form.is_valid(): # 验证email是否有效
email = request.POST.get('email', '')
if UserProfile.objects.get(email=email): # 判断用户是否存在,然后发邮件
send_register_email(email, "forget")
return HttpResponse("重置密码链接已发送到您的邮箱,请查收后点击重置.")
else:
return render(request, 'forget_pwd.html', {'msg': '用户不存在', 'forget_form': forget_form})
class ResetPwdView(View):
def get(self, request, active_code):
all_reocrds = EmailVerifyRecord.objects.filter(code=active_code)
if all_reocrds: # 如果链接有效
for record in all_reocrds:
email = record.email # 得到email, emailsendrecord表中有对应的
return render(request, 'reset_password.html', {'email': email}) # 将email带回, post到这个view: modify_pwd
else:
return HttpResponse("链接已失效...")
# ResetPwdView的post独立到ModifyPwdView里, 因为ResetPwdView post地址不好获取code
class ModifyPwdView(View):
def post(self, request):
modify_form = ModifyPwdForm(request.POST)
if modify_form.is_valid():
pwd1 = request.POST.get('password1', '')
pwd2 = request.POST.get('password2', '')
email = request.POST.get('email', '')#取出email,定位哪个用户
if pwd1 != pwd2: # 如果密码不一致, 则返回
return render(request, 'reset_password.html', {'msg': '密码不一致', 'email': email})
user = UserProfile.objects.get(email=email)
user.password = make_password(pwd2)
user.save()
return render(request, 'login.html')
else:
email = request.POST.get('email', '')
return render(request, 'reset_password.html', {'email': email, 'modify_form': modify_form})
处理逻辑