当前位置:网站首页>Django使用腾讯云发送短信并存入redis
Django使用腾讯云发送短信并存入redis
2022-08-04 12:22:00 【lyudev】
models.py
class UserInfo(models.Model):
username = models.CharField(verbose_name='用户名', max_length=32)
email = models.CharField(verbose_name='邮箱', max_length=32)
mobile_phone = models.CharField(verbose_name='手机号', max_length=32)
password = models.CharField(verbose_name='密码', max_length=32)
settings.py
# 短信的配置
TENCENT_SMS_APP_ID = 111 # 自己应用ID
TENCENT_SMS_APP_KEY = "222" # 自己应用Key
TENCENT_SMS_APP_SIGN = "333" # 自己腾讯云创建签名时填写的签名内容(使用公众号的话这个值一般是公众号全称或简称)
TENCENT_SMS_TEMPLATE = {
'login': 1344088,
'register': 1344087
}
tencent/sms.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
from qcloudsms_py import SmsMultiSender, SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
from django.conf import settings
def send_sms_single(phone_num, template_id, template_param_list):
"""
单条发送短信
:param phone_num: 手机号
:param template_id: 腾讯云短信模板ID
:param template_param_list: 短信模板所需参数列表,例如:【验证码:{1},描述:{2}】,则传递参数 [888,666]按顺序去格式化模板
:return:
"""
appid = settings.TENCENT_SMS_APP_ID # 自己应用ID
appkey = settings.TENCENT_SMS_APP_KEY # 自己应用Key
sms_sign = settings.TENCENT_SMS_APP_SIGN # 自己腾讯云创建签名时填写的签名内容(使用公众号的话这个值一般是公众号全称或简称)
sender = SmsSingleSender(appid, appkey)
try:
response = sender.send_with_param(86, phone_num, template_id, template_param_list, sign=sms_sign)
except HTTPError as e:
response = {'result': 1000, 'errmsg': "网络异常发送失败"}
return response
def send_sms_multi(phone_num_list, template_id, param_list):
"""
批量发送短信
:param phone_num_list:手机号列表
:param template_id:腾讯云短信模板ID
:param param_list:短信模板所需参数列表,例如:【验证码:{1},描述:{2}】,则传递参数 [888,666]按顺序去格式化模板
:return:
"""
appid = settings.TENCENT_SMS_APP_ID # 自己应用ID
appkey = settings.TENCENT_SMS_APP_KEY # 自己应用Key
sms_sign = settings.TENCENT_SMS_APP_SIGN # 自己腾讯云创建签名时填写的签名内容(使用公众号的话这个值一般是公众号全称或简称)
sender = SmsMultiSender(appid, appkey)
try:
response = sender.send_with_param(86, phone_num_list, template_id, param_list, sign=sms_sign)
except HTTPError as e:
response = {'result': 1000, 'errmsg': "网络异常发送失败"}
return response
forms/account.py
from django import forms
from django.core.exceptions import ValidationError
import random
from app import models
from django.core.validators import RegexValidator
from django.conf import settings
from utils.tencent.sms import send_sms_single
from django_redis import get_redis_connection
class RegisterModelForm(forms.ModelForm):
mobile_phone = forms.CharField(label='手机号', validators=[RegexValidator(r'^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$', '手机号格式错误。')])
password = forms.CharField(label='密码', widget=forms.PasswordInput())
confirm_password = forms.CharField(label='重复密码', widget=forms.PasswordInput())
code = forms.CharField(label='验证码', widget=forms.TextInput())
class Meta:
model = models.UserInfo
fields = ['username', 'email', 'password', 'confirm_password', 'mobile_phone', 'code']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
field.widget.attrs['placeholder'] = '请输入%s' % field.label
class SendSmsForm(forms.Form):
mobile_phone = forms.CharField(label='手机号', validators=[RegexValidator(r'^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$', '手机号格式错误。')])
def __init__(self, request, *args, **kwargs):
super().__init__(*args, **kwargs)
self.request = request
def clean_mobile_phone(self):
mobile_phone = self.cleaned_data['mobile_phone']
"""判断短信模板是否有问题"""
tpl = self.request.GET.get("tpl")
template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl)
if not template_id:
raise ValidationError("短信模板错误。")
exists = models.UserInfo.objects.filter(mobile_phone=mobile_phone).exists()
if exists:
raise ValidationError("手机号已存在。")
code = random.randrange(1000, 9999)
sms = send_sms_single(mobile_phone, template_id, [code, ])
if sms['result'] != 0:
raise ValidationError("短信发送失败{}".format(sms['errmsg']))
conn = get_redis_connection()
conn.set(mobile_phone, code, ex=60)
# print(conn.get('mobile_phone'))
return mobile_phone
views/account.py
from django.shortcuts import render, HttpResponse
from web.forms.account import RegisterModelForm, SendSmsForm
from django.http import JsonResponse
def register(request):
form = RegisterModelForm()
return render(request, 'register.html', {'form': form})
def smscode(request):
"""发送短信"""
form = SendSmsForm(request, data=request.GET)
if form.is_valid():
return JsonResponse({'status': True})
return JsonResponse({'status': False, 'error': form.errors})
urls.py
from django.urls import path ,re_path,include
from web.views import account
from app import views
urlpatterns = [
re_path(r'^send/sms/', views.send_sms, name='send_sms'),
re_path(r'^register/$', account.register, name='register'),
re_path(r'^send/smscode/$', account.smscode, name='send_smscode'),
]
前端
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 account">
<h3>用户注册</h3>
<form id="form" method="POST" novalidate>
{% csrf_token %}
{% for item in form %}
{% if item.name == 'code' %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ item.label }}</label>
<div class="row">
<div class="col-md-6 col-xs-6">
{{ item }}
</div>
<div class="col-md-6 col-xs-6">
<input type="button" id="smsBtn" class="btn btn-default" value="发送验证码">
</div>
</div>
</div>
{% else %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ item.label }}</label>
{{ item }}
<span class="error-msg"></span>
</div>
{% endif %}
{% endfor %}
<button type="submit" class="btn btn-primary">注册</button>
</form>
</div>
</div>
</div><script>
$(function () {
bindSmsBtn()
})
function bindSmsBtn() {
$('#smsBtn').on('click', function () {
$(".error-msg").empty();
var mobilePhone = $('#id_mobile_phone').val();
$.ajax({
url: "/send/smscode/",
data: {
"mobile_phone": mobilePhone,
"tpl": 'register'
},
type: "GET",
dataType:"JSON",
success: function (res) {
console.log(res)
if(res.status){
console.log("发送成功")
}
else{
console.log("sss")
$.each(res.error,function(key,value){
$("#id_"+key).next('span').text(value[0])
})
}
}
})
})
}
</script>
边栏推荐
- DC-DC电源中前馈电容的选择
- 编辑器vscode Already included file name ‘xxx‘ differs from file name ‘xxx‘ only in casing报错
- Focusing on data sources, data quality and model performance to build a credit profile of small and micro enterprises
- 树莓派入门
- #夏日挑战赛#OpenHarmony 给你的输入法加点彩—星球崛起
- 监督和半监督学习下的多标签分类综述
- MySQL索引原理以及SQL优化
- 力扣每日一题-第48天-345. 反转字符串中的元音字母
- 小程序在政务服务平台建设中如何发挥价值
- 什么是 DevOps?看这一篇就够了!
猜你喜欢
论文翻译:2022_Time-Frequency Attention for Monaural Speech Enhancement
yolo系列的Neck模块
exness:美联储重现鹰派口吻,黄金承压面临转跌信号
国际原子能机构总干事警告称扎波罗热核电站安全形势已“完全失控”
如何做好企业数字化转型?这10份靠谱案例收藏了(附下载)
动手学深度学习_LeNet
200PLC转以太网与研华webaccess modbusTCP客户端在空调机上应用配置案例
涨姿势了!原来这才是多线程正确实现方式
LeetCode Daily Question (858. Mirror Reflection)
Go编译原理系列8(变量捕获)
随机推荐
隐私计算与数据流通:关系、作用及功能
炫酷又高效的数据可视化大屏,做起来真的没那么难!丨极客星球
AI 助力双碳目标:让每一度电都是我们优化的
break与continue超详解!!!
聚焦数据来源、数据质量和模型性能构建小微企业信用画像
用VbScript控制光驱
Move the blog to CSDN
200PLC转以太网与研华webaccess modbusTCP客户端在空调机上应用配置案例
独立站卖家如何使用 WhatsApp Business API 建立有意义的客户关系?
能力更强,医疗单据识别+医疗知识库校验
什么是 DevOps?看这一篇就够了!
Nacos手摸手教学【二】Nacos注册中心
项目里的各种配置,你都了解吗?
考研数一数二数三之间的具体详细区别
A comprehensive understanding of MOS tubes, an article is enough
力扣解法汇总1403-非递增顺序的最小子序列
树莓派入门
防抖函数封装
Analysis and comparison of mobile cross-end technical solutions
Flutter教程大全合集(2022年版)