注册功能的激活链接

Posted chenyang13677

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了注册功能的激活链接相关的知识,希望对你有一定的参考价值。

1.在app目录下,创建一个包,utils

2.然后新建一个文件 emai_send.py

3.在文件中写入:

#coding:utf8

from random import Random
from django.core.mail import send_mail


from users.models import EmailVerifyRecord
from mytest.settings import EMAIL_FROM

‘‘‘生成随机码‘‘‘
def random_str(randomlength=8):
str = ‘‘
chars = ‘ABCDEFGHIJKLMNOPQRSTUVWSXZabcdefghijklmnopqrstuvwxyz0123456789‘
length = len(chars) - 1
random = Random()
for i in range(randomlength):
str += chars[random.randint(0,length)]
return str

‘‘‘发送确认邮件‘‘‘
def send_register_email(email,send_type=‘register‘):
email_record = EmailVerifyRecord()
if send_type == ‘update_email‘:
code = random_str(4)
else:
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
elif send_type == ‘update_email‘:
email_title = ‘慕学在线网邮箱重置链接‘
email_body = ‘您的邮箱验证码为:{0}‘.format(code)
send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
if send_status:
pass

4.在settings里面,在最后面加上:
EMAIL_HOST = ‘smtp.163.com‘   发送出去的邮箱类型
EMAIL_PORT = 25 发送的端口号
EMAIL_HOST_USER = ‘[email protected]‘ 发送的邮箱
EMAIL_HOST_PASSWORD = ‘pythonadc‘ 密码
EMAIL_USE_TLS = False
EMAIL_FROM = ‘[email protected]‘ 发件人

5.在views.py里面添加:
from utils.email_send import send_register_email
在该register类里面添加:
       send_register_email(user_name,‘register‘)
return render(request,‘login.html‘)


激活链接,注册成功
1.在urls.py文件里面:
from users.views import RegisterView, ActiveUserView
url(r‘^active/(?P<active_code>.*)/$‘, ActiveUserView.as_view(), name=‘user_active‘),
2.在views文件里面
from .models import UserProfile,EmailVerifyRecord
  
‘‘‘激活用户‘‘‘
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()
else:
return render(request,‘active_fail.html‘)
return render(request,‘login.html‘)


通常,这个是配合注册页面同时使用,如有疑问,请看看我的登陆页面的博客

以上是关于注册功能的激活链接的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot实现网站注册,邮件激活码激活功能

Joomla 注册和激活

Django 实现网站注册用户邮箱验证功能

PHP激活用户注册验证邮箱

如何激活注册的豆瓣网,说出全部的注册到激活的步骤

邮箱验证激活账号