码迷,mamicode.com
首页 > 其他好文 > 详细

[py][mx]django通过邮箱找回密码

时间:2018-02-03 15:49:56      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:逻辑   ali   流程   返回   png   修改   处理   response   技术分享   

忘记密码处理流程

技术分享图片

直接上代码

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

处理逻辑

技术分享图片

技术分享图片

[py][mx]django通过邮箱找回密码

标签:逻辑   ali   流程   返回   png   修改   处理   response   技术分享   

原文地址:https://www.cnblogs.com/iiiiiher/p/8408836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!