settings.py里面的配置

 1 # 邮件
 2 EMAIL_HOST = 'smtp.163.com'
 3 # 设置端口号,为数字
 4 EMAIL_PORT = 25
 5 #设置发件人邮箱
 6 EMAIL_HOST_USER = 'kongfuzi_k@163.com'
 7 # 设置发件人 授权码
 8 EMAIL_HOST_PASSWORD = '123456789hhh'
 9 # 设置是否启用安全链接
10 EMAIL_USER_TLS = True
11 # EMAIL_USER_TLS = False
12 DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
13 
14 # redis 的配置文件
15 CACHES = {
16     "default": {
17         "BACKEND": "django_redis.cache.RedisCache",
18         "LOCATION": "redis://127.0.0.1:6379",
19         "OPTIONS": {
20             "CLIENT_CLASS": "django_redis.client.DefaultClient",
21             "CONNECTION_POOL_KWARGS": {"max_connections": 100}
22             # "PASSWORD": "密码",
23         }
24     }
25 }

 

views.py里面的代码 有存入redis 和 发送邮件

 1 from django.core.cache import cache  # 存入redis
 2 from rest_framework.views import APIView
 3 from rest_framework.response import Response
 4 from shiyanlou_dj import settings
 5 from random import randint
 6 import hashlib
 7 
 8 #生成随机的验证码
 9 def Email_Code():
10     code_ = ''
11     code_str = 'abcdefghijklmnopqrstuvwxyz1234567890'
12     for k in range(5):
13         code_ += code_str[randint(0,35)]
14     return code_
15 
16 #   md5加盐
17 def md5_md5(params):
18     md5 = hashlib.md5(params.encode(encoding='utf-8'))
19     return md5.hexdigest()
20 class RegisterView(APIView):
21     def post(self, request):
22         try:
23             email = request.data.get('email')  # 获取前端邮箱
24             code = request.POST.get('verif') #获取前段的验证码
25             # 验证邮箱的格式
26             re_email = r'(\w+)@(\w+)\.(\w+)'
27             if not re.match(re_email, email):
28                 return Response({'code': 4001, 'msg': '邮箱格式不正确'})
29             #判断是否输入了验证码
30             if not code:
31                 return Response({'code':4003,'msg':'请输入验证码'})
32             #获取验证码,并判断是否失效了
33             code_ = cache.get('syl_'+email)
34             if not code_:
35                 return Response({'code':4006,'msg':'验证码已失效'})
36             #判断输入的验证码,和获取到的验证码是否一致
37             if code == code_:
38                 data = {
39                     'email':email,
40                     'password':password,
41                     'username':email,
42                     'img':'666'
43                 }
44                 #   序列化写入数据
45                 userser = UserSerializer(data=data)
46                 if not userser.is_valid():
47                     return Response({'code':4005,'msg':'注册失败'})
48                 ret = {
49                     'code': 200,
50                     'msg': '注册成功'
51                 }
52                 userser.save()   #提交
53                 return Response(ret)
54 
55             return Response({'code':4004,'msg':'验证码错误'})
56         #返回错误信息
57         except:
58             return Response({'code': 0, 'msg': '网络有些问题,请等一下再试'})
59 
60 
61 # post请求
62 # 获取到验证码,并存入redis
63 # 发生邮箱
64 class Email_code_APIView(APIView):
65     def post(self, request):
66         try:
67             # 从前端获取验证码
68             email = request.POST.get('email')
69             # 生成随机的验证码
70             code = Email_Code()
71             ret = '您的验证码是{}'.format(code)
72             # 给邮箱发送验证码
73             my_email = send_mail('激活验证', ret, settings.DEFAULT_FROM_EMAIL, [email])
74             if not my_email == 1:
75                 return Response({
76                     'code': 0,
77                     'msg': "邮件发送失败",
78                 })
79             key = 'syl_' + email
80             cache.set(key, code, 30)  # 5分钟的有效时间
81             ret = {
82                 'code': 200,
83                 'msg': "邮件发送成功"
84             }
85             return Response(ret)
86         # 返回错误信息
87         except:
88             return Response({'code': 0, 'msg': '网络有些问题,请等一下再试'})

 

 

 记得刚开始的时候打开redis 要不然你会一直的报错????????????