1.在urls.py里面添加:
from users.views import RegisterView, ActiveUserView, ForgetPwdView
url(r'forget/$', ForgetPwdView.as_view(), name='forget_pwd'),
2.在views.py文件里面:
'''找回密码'''
class ForgetPwdView(View):
def get(self,request):
forget_form = ForgetForm()
return render(request,'forgetpwd.html',{'forget_form':forget_form},)
def post(self,request):
forget_form = ForgetForm(request.POST)
if forget_form.is_valid():
email = request.POST.get('email',None)
print(email)
send_register_email(email,'forget')
return render(request,'send_success.html')
else:
return render(request, 'forgetpwd.html',{'forget_form':forget_form},)
3.在forms.py文件里面添加:
class ForgetForm(forms.Form):
email = forms.EmailField(required=True)
captcha = CaptchaField(error_messages={'invalid':u'验证码错误'})
4.在app/utils/email_send.py文件里面:
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
5.在html文件下的 验证码下面添加:
{{ register_form.captcha }} 要是这个样子出错的话,就改用HTML文本:
找到表单数据,在form里面加入: action="{% url 'login' %}" method="post"
在表单结尾之前加入:{% csrf_token %}
注意,表单里面的name属性要和views.py文件里面要传递的属性要相同
判断返回页面的样式,是否显示的是登陆状态,
{% if register_form.errors.password %}errorput{% endif %}
新建一个div 来放置报错信息:
{% for key,error in register_form.errors.items%}{{ error }}{% endfor %}{{ msg }}
6.为了用户体验好,我们在form表单里面添加一个value属性
value='{{ register_form.email.value }}'
7.在urls.py文件配置路由,来处理找回密码的链接
url(r'^reset/(?P.*)/$', ResetView.as_view(), name='reset'),
8.在views.py文件立里面添加:
'''找回密码'''
class ResetView(View):
def get(self,request,active_code):
all_records = EmailVerifyRecord.objects.filter(code=active_code)
if all_records:
for record in all_records:
email = record.email
return render(request, 'password_reset.html',{'email':email},)
else:
return render(request,'active_fail.html')
return render(request,'login.html')
9.在forms.py里添加:
class ModifyPwdForm(forms.Form):
email = forms.EmailField(required=True)
password = forms.PasswordInput()
password2 = forms.PasswordInput()
10.在html文件里面添加 :
在表单结尾之前加入:{% csrf_token %}
11.在views.py里面:
'''验证密码'''
class ModifyPwdView(View):
def post(self,request):
modify_form = ModifyPwdForm(request.POST)
if modify_form.is_valid():
pwd1 = request.POST.get('password1',None)
pwd2 = request.POST.get('password2', None)
email = request.POST.get('email','1')
print (email)
if pwd1 != pwd2:
return render(request,'password_reset.html',{'email':email,'msg':'两次密码不相同'})
user = UserProfile.objects.get(email=email)
user.password = make_password(password=pwd2)
user.save()
return render(request,'login.html')
else:
email = request.POST.get('email', None)
return render(request, 'password_reset.html', {'email': email, 'modify_form':modify_form})
12.在urls.py里面:
url(r'modify_pwd/$', ModifyPwdView.as_view(), name='modify_pwd'),
13.在HTML文件里面:
找到表单数据,在form里面加入: action="{% url 'modify_pwd' %}" method="post"