• 用户登录机制(找回密码)


    用户登录机制(找回密码)

    一、渲染过程

    1.urls.py文件

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url('^$',TemplateView.as_view(template_name="index.html"),name='index'),
        url(r'^login/$',views.User_login.as_view(),name='login' ),
        url(r'^reg/$',views.User_reg.as_view(),name='reg' ),
        url(r'^active/(?P<acctive_code>.*)/$',views.AciveUserView.as_view(),name='user_active'),
        url(r'^forget/$',views.ForgetPwdView.as_view(),name='forget_pwd'), #用户忘记密码url
    ]
    

    2.forms.py文件

    class ForgetForm(forms.Form):
    
        email = forms.EmailField(required=True)
        captcha = CaptchaField(error_messages={"invalid":"验证码错误"})  #直接引用
    

    3.views.py文件

    class ForgetPwdView(View):
    	def get(self,request):
    		forget_form = forms.ForgetForm()
    		return  render(request,'forgetpwd.html',{"forget_form":forget_form})
    

    4.前端渲染的效果如图所示

    二、重置过程

    1.email_send.py文件

    def random_str(randomlength=8):
        str = ''
        chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz123456789'
        lengh = len(chars) - 1
        random = Random()
        for i in range(randomlength):
            str+=chars[random.randint(0,lengh)]
        return str
    def send_register_email(email,send_type="register"):
        print("进去邮箱")
        email_record = EmailVerifyRecord()
        code =random_str(16)
        email_record.code = code
        email_record.email =email
        email_record.send_type = send_type
        email_record.save()
    
        ###发送邮件###
        email_title = "" #邮件的title
        email_body = "" #邮件的正文
    
        if send_type == "register":
    
            email_title = "苹果官网注册激活链接"
            email_body = "请点击下面的链接激活你的账号:http://127.0.0.1:8000/active/{0}".format(code)
    
            send_status = send_mail(email_title,email_body,EMAIL_FROM,[email])
            if send_status:
                pass
        elif send_type == "forget":  #用户忘记密码
            email_title = "苹果官网重置密码链接"
            email_body = "请点击下面的链接重置你的密码:http://127.0.0.1:8000/reset/{0}".format(code)
            send_status = send_mail(email_title,email_body,EMAIL_FROM,[email])
            if send_status:
                pass
    

    2.views.py文件

    class ForgetPwdView(View):
    	def get(self,request):
    		forget_form = forms.ForgetForm()
    		return  render(request,'forgetpwd.html',{"forget_form":forget_form})
    	def post(self,request):
    		forget_form = forms.ForgetForm(request.POST)
    		if forget_form.is_valid():
    			email = request.POST.get("email",'')
    			send_register_email(email,'forget')  #发送重置密码邮件
    			return render(request,'send_success.html') #该页面用于提示用户邮件发送成功
    		else:
    			return  render(request,'forgetpwd.html',{"forget_form":forget_form})
    
    ###用来验证邮箱后处理的视图函数####
    
    
    class ResetView(View):
    	def get(self,request,active_code):
    		all_records = models.EmailVerifyRecord.objects.filter(code=active_code)
    		if all_records:
    			for record in all_records:
    				email = record.email  #查找到验证码
    				return render(request,'pasword_reset.html',{"email":email}) #传email的可以判断是哪个用户重置密码需要在前端渲染一个隐藏的input框
    
    		return render(request,'login.html')
    
    ####post提交####
    
    class ModifypwdView(View):
    	def post(self,request):
    		modify_form = forms.ModifyPwdForm(request.POST)
    		if modify_form.is_valid():
    			pwd1 = request.POST.get("password1",'')
    			pwd2 = request.POST.get("password2",'')
    			email = request.POST.get("email","")
    			if pwd1 != pwd2:
    				return render(request,"password_reset.html",{"email":email,"msg":"密码不一致"})
    			user = models.Userinfo.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,"password_reset.html",{"email":email,'modify_form':modify_form})
    

    3.urls.py文件

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url('^$',TemplateView.as_view(template_name="index.html"),name='index'),
        url(r'^login/$',views.User_login.as_view(),name='login' ),
        url(r'^reg/$',views.User_reg.as_view(),name='reg' ),
        url(r'^active/(?P<acctive_code>.*)/$',views.AciveUserView.as_view(),name='user_active'),
        url(r'^forget/$',views.ForgetPwdView.as_view(),name='forget_pwd'), #用户忘记密码url
        url(r'^reset/(?P<acctive_code>.*)/$',views.AciveUserView.as_view(),name='user_reset'), #用来处理邮箱验证后的重置url
    	url(r'^modify_pwd/$',views.ModifyPwdView.as_view(),name='modify_pwd')  #用来处理邮箱验证中的POST提交的url
    ]
    

    4.前端页面

    前端页面需要注意的url

  • 相关阅读:
    The Water Problem(排序)
    Alisha’s Party(队列)
    The Hardest Problem Ever(字符串)
    火烧赤壁
    Jquery操作一遍过
    Django之认证系统
    Mysql漂流系列(一):MySQL的执行流程
    RESTful架构&简单使用Django rest framework
    git&github快速掌握
    jsonp突破浏览器同源策略
  • 原文地址:https://www.cnblogs.com/lijian-22huxiaoshan/p/7860693.html
Copyright © 2020-2023  润新知