- 首先要调用发送邮件的函数,要成功的发送跳转地址,这里的跳转地址要储存在数据库中,以便以后可以知道是哪个账号要进行重置密码
__author__ = 'LelandYan'
__date__ = '2018/9/17 13:38'
from random import Random
from django.core.mail import send_mail
from users.models import EmailVerifyRecord
from MxOnline.settings import EMAIL_FROM
def send_register_email(email, send_type="register"):
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 = ""
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
def random_str(randomlength=8):
str = ''
chars = 'abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
length = len(chars) - 1
random = Random()
for i in range(randomlength):
str += chars[random.randint(0, length)]
return str
- views.py中书写俩个视图类其中一个为对发送的邮件的网址的解析
其中html中还应该有form表单,然后是form表单跳转的页面,要判断密码是否修改成功
这里注意一个为get一个为post
class RestView(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', {})
class ModifyView(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', '')
if pwd1 != pwd2:
return render(request, 'password_reset.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, 'password_reset.html', {'modify_form': modify_form, 'email': email})
def get(self, request):
return render(request, 'index.html')
- 还要对url进行配置
url(r'^reset/(?P.*)/$', RestView.as_view(), name='reset_pwd'),
url(r'^modify/$', ModifyView.as_view(), name='modify_pwd'),
- 这里注意这里视图类必须要两个,不可以合起来,因为第一个url在html中要携带参数,所以定义了俩个试图类