Django使用腾讯云发送短信并存入redis
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)
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)
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 class="col-md-6 col-xs-6">
<input type="button" id="smsBtn" class="btn btn-default" value="发送验证码">
{% else %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ item.label }}</label>
{{ item }}
<span class="error-msg"></span>
{% endif %}
{% endfor %}
<button type="submit" class="btn btn-primary">注册</button>
</form>
</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])