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)

如果,你对上面的内容还有疑问,推荐选择西部数码企业云邮箱!有专人协助您解答邮箱疑问。

西部数码21年老牌服务商,企业邮箱功能丰富,如定时发送、支持层级子文件夹,邮件撤回,日程微信通知、多彩便签等几十项特色功能,使用管理便捷。按需自由定制,购买灵活,PCIe加速、极速收发!而且支持小程序收发邮件,随时随地移动办公。价格实惠,还可以免费试用!

高性价比企业邮箱开通链接:https://www.west.cn/services/mail/

赞(0)
声明:本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-62778877-8306;邮箱:fanjiao@west.cn。本站原创内容未经允许不得转载,或转载时需注明出处:西部数码知识库 » Django 注册邮箱验证

登录

找回密码

注册