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

Posted 毛台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[py][mx]django通过邮箱找回密码相关的知识,希望对你有一定的参考价值。

忘记密码处理流程

注意: 这个文章里的找回密码页面截取有问题. 找回密码页应该是让输入邮箱

直接上代码

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通过邮箱找回密码的主要内容,如果未能解决你的问题,请参考以下文章

邮箱验证修改密码,通过邮箱找回密码

Java实现邮箱找回密码

php+jQuery+Mysql找回密码----ThinkPHP

给自己项目添加注册登陆改密码邮箱找回密码等功能,出现大坑!

[py][mx]django自定义认证类

通过邮件找回密码