如何解决如何将 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 举报,一经查实,本站将立刻删除。