重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Django注册邮箱验证

settings.py

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、虚拟空间、营销软件、网站建设、夷陵网站维护、网站推广。

EMAIL_HOST = "smtp.126.com" EMAIL_POSRT = 25 EMAIL_HOST_USER = ""#邮箱名 EMAIL_HOST_PASSWORD = ""#密码 EMAIL_USE_TLS = False EMAIL_FROM = ""#邮箱名

建立utils文件夹新建send_email.py
生成随机数作为激活链接

from random import Random

def generate_random_str(randomlength=8): str = \'\' chars = \'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789\' length = len(chars)-1 random = Random() for i in range(randomlength): str+=chars[random.randint(0,length)] return str

发送邮件

from xxx.settings import EMAIL_FROM#导入smtp

from users.models import EmailVerifyRecord #导入模型

from django.core.mail import send_mail #导入发送邮件

def send_register_email(email,send_type="register"): email_record = EmailVerifyRecord()#实例化 random_str = generate_random_str(16)#取随机数 email_record.code = random_str email_record.email = email email_record.send_type = send_type email_record.save() if send_type == "register": email_title = "注册激活链接" email_body = "请点击下面的链接激活你的账号:http://127.0.0.1:8000/active/{0}".format(random_str) send_status = send_mail(email_title,email_body,EMAIL_FROM,[email]) if send_status: pass
app.views.py

from utils.email_send import send_register_email

from .forms import LoginForm

class RegisterView(View): def get(self,request):#访问 return render(request,"register.html") def post(self,request):#登陆 register_form = RegisterForm(request.POST) if register_form.is_valid():#表单验证 email = request.POST.get("email", "")

if UserProfile.objects.filter(email = email): return render(request, "register.html",{"register_form":register_form,"msg": "用户已经存在"}) pass_word = request.POST.get("password", "") user_profile = UserProfile()#实例化用户数据库 user_profile.username = email user_profile.email = email user_profile.is_active = False#邮箱验证状态 user_profile.password = make_password(pass_word)#密码生成 user_profile.save()#保存数据 send_register_email(email,"register")#发送邮件 return render(request,"login.html") else: return render(request, "register.html",{"register_form":register_form})

激活用户邮箱:.

app.views.py登陆视图

class LoginView(View): def get(self,request): return render(request, "login.html", {}) def post(self,request): login_form = LoginForm(request.POST) if login_form.is_valid():#验证表单填写信息是否有效 user_name = request.POST.get("username", "") pass_word = request.POST.get("password", "") user = authenticate(username=user_name, password=pass_word) if user is not None:#判断是否匹配 if user.is_active:#判断用户是否已激活 login(request, user)#登陆 return render(request, "index.html") else: return render(request, "login.html", {"msg": "邮箱未激活"})#未激活提示 else: return render(request, "login.html", {"msg": "用户名或密码错误"})#错误提示 else: return render(request,"login.html",{"login_form":login_form})#表单提示

app.views.py激活

#激活用户邮箱 class ActiveUserView(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 user = UserProfile.objects.get(email=email) user.is_active = True user.save() return HttpResponse("The param is : " + active_code)


本文名称:Django注册邮箱验证
网页地址:http://cqcxhl.cn/article/cgsssg.html
在线咨询
服务热线
服务热线:028-86922220
TOP