这个6-8对应对应6-11,6-12
拷入forgetpassword页面
书写处理忘记密码的view
users/views.py
# 用户忘记密码的处理view
class ForgetPwdView(View):
# get方法直接返回页面
def get(self, request):
return render(request, "forgetpwd.html", { })
django2.0.1 urls中配置:
# 忘记密码
path('forget/', ForgetPwdView.as_view(), name= "forget_pwd"),
Django1.9.8 urls中配置:
# 忘记密码
url('forget/$', ForgetPwdView.as_view(), name="forget_pwd"),
login html中忘记密码
![](http://upload-images.jianshu.io/upload_images/1779926-d744609e96a86e01.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/395)
mark
配置忘记密码页面中静态文件。
load static
修改static的目录
修改其中的url
定义一个给forget的form
users/forms.py:
# 注册验证码实现
class ForgetForm(forms.Form):
# 此处email与前端name需保持一致。
email = forms.EmailField(required=True)
# 应用验证码 自定义错误输出key必须与异常一样
captcha = CaptchaField(error_messages={"invalid": u"验证码错误"})
添加验证码
# 用户忘记密码的处理view
class ForgetPwdView(View):
# get方法直接返回页面
def get(self, request):
forget_from = ForgetForm()
return render(request, "forgetpwd.html", {"forget_from":forget_from })
html中加上验证码
![](http://upload-images.jianshu.io/upload_images/1779926-ac402410d27431d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/391)
mark
post中逻辑
# post方法实现
def post(self, request):
forget_form = ForgetForm(request.POST)
# form验证合法情况下取出email
if forget_form.is_valid():
email = request.POST.get("email","")
# 发送找回密码邮件
send_register_eamil(email, "forget")
# 发送完毕返回登录页面并显示发送邮件成功。
return render(request, "login.html", {"msg":"重置密码邮件已发送,请注意查收"})
# 如果表单验证失败也就是他验证码输错等。
else:
return render(request, "forgetpwd.html", {"forget_from": forget_form })
邮箱重置密码邮件发送
elif send_type == "forget":
email_title = "mtianyan慕课小站 找回密码链接"
email_body = loader.render_to_string(
"email_forget.html", # 需要渲染的html模板
{
"active_code": code # 参数
}
)
msg = EmailMessage(email_title, email_body, EMAIL_FROM, [email])
msg.content_subtype = "html"
send_status = msg.send()
![](http://upload-images.jianshu.io/upload_images/1779926-37697402c2c1d340.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/684)
mark
前端页面添加错误信息
已经重复很多遍这个操作了。
![](http://upload-images.jianshu.io/upload_images/1779926-ca1308d1a60c0514.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/451)
mark
![](http://upload-images.jianshu.io/upload_images/1779926-710fe9c11f1c0592.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
mark
![](http://upload-images.jianshu.io/upload_images/1779926-9862123a3651aacd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
mark
上述三图进行改正,不一一列举
书写重置密码view
# 重置密码的view
class ResetView(View):
def get(self, request, active_code):
# 查询邮箱验证记录是否存在
all_record = EmailVerifyRecord.objects.filter(code=active_code)
# 如果不为空也就是有用户
active_form = ActiveForm(request.GET)
if all_record:
for record in all_record:
# 获取到对应的邮箱
email = record.email
# 将email传回来
return render(request, "password_reset.html", {"email":email})
# 自己瞎输的验证码
else:
return render(
request, "forgetpwd.html", {
"msg": "您的重置密码链接无效,请重新请求", "active_form": active_form})
配置重置密码url
# Django1.9.8:
# 重置密码urlc :用来接收来自邮箱的重置链接
url('reset/(?P<active_code>.*)/$', ResetView.as_view(), name="reset_pwd"),
# django2.0.1:
# 重置密码urlc :用来接收来自邮箱的重置链接
re_path('reset/(?P<active_code>.*)/', ResetView.as_view(), name="reset_pwd"),
拷贝进来password reset页面
![](http://upload-images.jianshu.io/upload_images/1779926-50e2bac45215f39b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/448)
mark
添加一个隐藏的input框,以便于我们知道到底是哪个用户在重置密码
![](http://upload-images.jianshu.io/upload_images/1779926-e56d852474ebd343.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/423)
mark
配置html中三大变化加url配置。
![](http://upload-images.jianshu.io/upload_images/1779926-c1257a82940c3eec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/619)
mark
reset
的url
需要我们传参进来,但是modify
的不需要。
所以url配置和view都得分开。
创建改变密码的forms:
# 重置密码form实现
class ModifyPwdForm(forms.Form):
# 密码不能小于5位
password1 = forms.CharField(required=True, min_length=5)
# 密码不能小于5位
password2 = forms.CharField(required=True, min_length=5)
书写改变密码的view;
# 改变密码的view
class ModifyPwdView(View):
def post(self, request):
modiypwd_form = ModifyPwdForm(request.POST)
if modiypwd_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 = UserProfile.objects.get(email=email)
# 加密成密文
user.password = make_password(pwd2)
# save保存到数据库
user.save()
return render(request, "login.html", {"msg": "密码修改成功,请登录"})
# 验证失败说明密码位数不够。
else:
email = request.POST.get("email", "")
return render(request, "password_reset.html", {"email": email, "modiypwd_form":modiypwd_form})
配置modify的url。
django2.0.1:
# 修改密码url; 用于passwordreset页面提交表单
path('modify_pwd/', ModifyPwdView.as_view(), name="modify_pwd"),
django1.9.8:
# 修改密码url; 用于passwordreset页面提交表单
url(r'^modify_pwd/$', ModifyPwdView.as_view(), name="modify_pwd"),
建议自行走一遍注册,登录,忘记密码。重置密码。
错误的激活链接,错误的重置链接。值回填,form报错
更多: 重置密码链接是否被点击过,过期时间。
对应commit忘记密码重置功能实现完毕,并进行了必要的测试。对应6-11,6-12
原文学习来自简书,作者:天涯明月笙
链接:https://www.jianshu.com/p/5f280d6ea033