前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Django使用腾讯云发送短信并存入redis

Django使用腾讯云发送短信并存入redis

作者头像
lyudev
发布2022-08-04 10:57:21
1.9K0
发布2022-08-04 10:57:21
举报
文章被收录于专栏:代码即数据

models.py

代码语言:javascript
复制
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

代码语言:javascript
复制
# 短信的配置
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

代码语言:javascript
复制
#!/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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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'),
]

前端

代码语言:javascript
复制
<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>
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 代码即数据 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档