300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Django--手机号注册生成验证码

Django--手机号注册生成验证码

时间:2023-10-13 03:41:37

相关推荐

Django--手机号注册生成验证码

Django–手机号注册生成验证码

文章目录

Django--手机号注册生成验证码一、使用互亿无线短信服务二、新建一个HTML页面三、view界面四、新建一个tools.py文件注意:

一、使用互亿无线短信服务

1.注册互亿无线账户(免费10条短信)

网址:/

2.点击验证码通知短信–>使用向导–>DEMO示例–>python

二、新建一个HTML页面

##############前端============== 表单1.输入框 1.手机号 2.验证码 3.密码2.两个按钮 1.获取验证 2.提交表单 ==============1. 一个手机号 点击获取验证码 (**不是使用form表单提交) ---->ajax异步 发送邮箱(导入jquery文件) 2. 手机号 验证码 密码 提交表单 验证注册 {% load static %}<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>手机号注册</title><script src="/static/jq/jquery.min.js"></script></head><body><form action="" method="post" style="text-align: center">{% csrf_token %}<div>手机号:<input type="text" name="phone" placeholder="请输入手机号" required="" id="phone_id" class="input"/></div><div>验证码:<input type="text" name="yz_code" placeholder="请输入验证码" required="" style='width: 150px' class="input" id="code"/><input class="huoqu" id="zphone" type="button" onclick="send_phone()" value=" 获取验证码 "></div><div>密码:<input type="password" name="password" placeholder="" required="" class="input"/></div><div><input type="submit" id="submit" style="align:center" value="注册" class="gouwu"/></div></form>{% if error_msg %}<p style="text-align: center; color: red;">{{ error_msg }}</p>{% endif %}{% if succeed_msg %}<p style="text-align: center; color: red;">{{ succeed_msg }}</p>{% endif %}</body><script>function send_phone() {$.ajax({url:'http://127.0.0.1:8000/buyer/phone_register/',type:'get',data:'phone=' + document.getElementById('phone_id').value,success(){alert('发送成功')}})}</script></html>

三、view界面

class Register_phone(View):def get(self, request):# 失败信息error_msg = ''# 成功信息succeed_msg = ''# 获取手机号phone = request.GET.get('phone')# 判断手机号是否注册is_seller = models.Buyer.objects.filter(phone=phone).first()if is_seller:# 失败信息error_msg = '手机号已注册'return render(request, 'buyer/register_phone.html', locals())if phone:# 利用 tools.py 内 send_note方法 发送短信send_p = send_note(phone)if send_p == 2:# 成功信息succeed_msg = '发送成功'return render(request, 'buyer/register_phone.html', locals())else:# 失败信息error_msg = send_preturn render(request, 'buyer/register_phone.html', locals())return render(request, 'buyer/register_phone.html', locals())def post(self, request):# 失败信息error_msg = ''# 成功信息succeed_msg = ''phone = request.POST.get('phone')yz_code = request.POST.get('yz_code')password = request.POST.get('password')# - 判断 手机号 密码不能为空if phone == '' or yz_code == '' or password == '':# 失败信息error_msg = '用户信息不能为空'return render(request, 'buyer/register_phone.html', locals())# - 判断 手机号是否注册is_buyer = models.Buyer.objects.filter(phone=phone).first()if is_buyer:# 失败信息error_msg = '用户注册'return render(request, 'buyer/register_phone.html', locals())# - 验证码判断if not REDIS_CON.get('PHONE_' + str(phone)):# 失败信息error_msg = '验证码已失效'return render(request, 'buyer/register_phone.html', locals())if REDIS_CON.get('PHONE_' + str(phone)).decode('utf-8') != yz_code:# 失败信息error_msg = '验证码不正确'return render(request, 'buyer/register_phone.html', locals())# - 注册成功 写入数据库models.Buyer.objects.create(phone=phone, password=make_password(password)).save()# 返回成功信息succeed_msg = '注册成功'return render(request, 'buyer/register_phone.html', locals())

四、新建一个tools.py文件

import requestsimport randomfrom shop.settings import REDIS_CONimport json#定义随机验证码def yz_code(phone):res = ''for i in range(6):rand = random.randint(0, 9)res += str(rand)REDIS_CON.set('PHONE_'+str(phone), res)REDIS_CON.expire('PHONE_'+str(phone), 60)return res#定义发送短信 方法def send_note(phone):#三方 urlurl = '/webservice/sms.php?method=Submit'# APPIDaccount = 'C97682185'# APPKEYpassword = '39adb9c339bafec3cb47e6716915aff1'# 接受方手机号mobile = phone# 短信内容content = f'您的验证码是:{yz_code(phone)}。请不要把验证码泄露给其他人。'#返回格式format = 'json'data = {'account': account,'password': password,'mobile': mobile,'content': content,'format': format}req = requests.post(url=url, data=data).content.decode('utf-8')if json.loads(req)['code'] == 2:return 2else:return json.loads(req)['msg']

注意:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。