如何将 OTP 发送到 Django 中的电话号码?

问题描述

我需要在提交表单时验证电话号码(无需登录注册)。我正在使用 Twilio 服务。请给我建议我该怎么做。我需要通过填写表格中的数字将 OTP 发送给用户,然后验证然后提交表格。提前致谢!

我的模型:

class Booking(models.Model):
            date                        = models.CharField(max_length=200,blank=True,db_index=True,null=True )
            time                        = models.CharField(max_length=200,null=True )
            name                        = models.CharField(max_length=200,null=True )
            email                       = models.CharField(max_length=200,null=True )
            product                     = models.CharField(max_length=200,null=True )
            phone                       = models.IntegerField(max_length=200,null=True )
            address                     = models.TextField(max_length=200,null=True ) 
    
    otp                     = models.CharField(max_length=6,null=True )

我的观点

import os
from twilio.rest import Client
from django.shortcuts import render,get_object_or_404,redirect
from .models import Product,Technician,FAQ,Booking

def booking(request,slug=None,pk=None):
    products = get_object_or_404(Product,slug=slug)
    if request.method == 'POST':
        date = request.POST['date']
        time = request.POST['time']
        name = request.POST['name']
        email = request.POST['email']
        product = request.POST['product']
        phone = request.POST['phone']
        address = request.POST['address']
        otp = ran()
                
        data  = Booking(date=date,time=time,name=name,email=email,product=product,phone=phone,address=address,otp=otp)
        data.save()
        send_otp(phone,otp)
        request.session['phone'] = phone
        return redirect('otp')       

    else:
        return render(request,'book.html',{'product' : products})


def send_otp(phone,otp):
    account_sid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
    auth_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    client = Client(account_sid,auth_token)
    message = client.messages \
            .create(
                     body="Join Earth's mightiest heroes. Like Kevin Bacon.",from_='+xxxxxxxxx',to=phone
                 )
    return message


def otp(request):
    phone = request.session['phone']
    return render(request,'otp.html',{'phone':phone})

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)