django注册用邮箱发送验证码的实现
作者:舔舔柠檬 时间:2022-11-08 06:59:59
视图代码
lis = []#设置一个空列表用来存放发送的验证码,用来验证
def yzm1():
res1 = ""
for i in range(4):#用四个随机数组成验证码
num = random.randint(0, 9)
res1 += str(num)
lis.append(res1)#将验证码放入空的列表中
return res1#返回验证码
class zc(View):
def get(self, request):
return render(request, 'zc.html', locals())
def post(self, request):
name = request.POST['qq']#获取前端传递进来的数据
subject = '验证码'
message = vercode()#获取验证码
form = '837620306@qq.com'#获取发送验证码的邮箱
# EMAIL_HOST_USER
rcipient_list = [name]
res = send_mail(subject=subject, message=message, from_email=form, recipient_list=rcipient_list)#发送验证码返货真或假,假代表没有发送成功
if res == 1:#判断是否发送成功
return redirect('/app01/yzm/')#成功的话跳转到验证的网页
else:#不成功的话返回注册界面
return render(request, 'zc.html', locals())
def yzm2(request):
if request.method == 'GET':
return render(request, 'yzm.html', locals())
elif request.method == 'POST':
a = request.POST['much']#获取到用户输入的验证码
if a == lis[-1]:#取最新的验证码
return HttpResponse('验证码正确')
else:
return HttpResponse('no')
用来发送验证码的邮箱需要打开POP3/SMTP服务和IMAP/SMTP服务,并且获得POP3/SMTP服务的授权码
settings 配置文件
另外需要对django的settings文件进行设置
# 设置发送邮件服务器:smtp.qq.com,
EMAIL_HOST = 'smtp.qq.com'
# 设置端口号,如果使用的是SSL,端口号为465或587
EMAIL_PORT = 25
#设置发件人邮箱
EMAIL_HOST_USER = 'xxxxx'
# 设置发件人 授权码 (POP3/SMTP服务的授权码)
EMAIL_HOST_PASSWORD = 'xxxxx'
# 设置是否启用安全链接
EMAIL_USER_TLS = True
来源:https://blog.csdn.net/weixin_46643772/article/details/115734722
标签:django,邮箱,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现的旋转数组功能算法示例
2021-09-11 20:49:46
完美解决Python2操作中文名文件乱码的问题
2022-12-12 11:29:46
![](https://img.aspxhome.com/file/2023/2/90832_0s.jpg)
Js中的函数直接量
2007-12-21 19:15:00
Python 多线程超详细到位总结
2021-02-17 04:10:58
做设计还是做产品
2009-06-11 13:01:00
python内置函数sorted()用法深入分析
2022-06-26 07:41:40
可能被你忽略的 JavaScript 代码陷阱
2009-12-26 18:16:00
window.opener用法和用途实例介绍
2024-04-17 10:38:13
![](https://img.aspxhome.com/file/2023/6/136216_0s.gif)
vue+el使用this.$confirm,不能阻断代码往下执行的解决
2024-05-29 22:43:42
Python对接六大主流数据库(只需三步)
2022-04-25 21:51:55
![](https://img.aspxhome.com/file/2023/4/64674_0s.jpg)
anaconda navigator打不开问题的解决方法
2023-09-12 08:48:20
![](https://img.aspxhome.com/file/2023/2/118062_0s.jpg)
sql随机记录之如何解决每次RND的结果都一样的问题?
2007-09-11 13:24:00
Python将文字转成语音并读出来的实例详解
2021-11-08 21:23:34
![](https://img.aspxhome.com/file/2023/3/131443_0s.jpg)
Python基于回溯法子集树模板解决野人与传教士问题示例
2023-07-14 04:36:05
![](https://img.aspxhome.com/file/2023/8/60948_0s.jpg)
Python 中 Meta Classes详解
2023-06-02 11:52:47
mysql数据库锁的产生原因及解决办法
2024-01-20 14:09:46
Python中的变量及简单数据类型应用
2021-06-13 09:45:26
![](https://img.aspxhome.com/file/2023/5/71365_0s.png)
用Python逐行分析文件方法
2022-07-10 10:17:49
js父页面中使用子页面的方法
2024-04-19 09:50:49
Python包装之对象处理
2022-10-15 07:01:33
![](https://img.aspxhome.com/file/2023/1/100931_0s.jpg)