如何实现在手机上发送otp?

时间:2019-10-19 12:21:53

标签: python django twilio one-time-password

我正在尝试使用twilio,python和django进行电话注册

我无法理解如何实现机制,该机制需要用户输入来向他发短信

生成otp并发送给用户:

from twilio.rest import Client
import random
otp=random.randint(1000,9999)


account_sid = ''
auth_token = ''

client = Client(account_sid, auth_token)

client.messages.create(from_='+',
                      to='+',
                      body='Your OTP is -'+str(otp))

用户输入他的电话号码后,它将发送到服务器

但是当我将他的号码发送到服务器时如何将他的号码放在to =" _HERE_ "上,然后如何调用该文件?

1 个答案:

答案 0 :(得分:1)

在您的视图中,只需获取用户输入并将其发送到positional argument

views.py

from django.views import View


class SendOTP(View):
    def post(self, request):
        if request.method == "POST":
            to = request.POST.get('to')
            _from = request.POST.get('from')
            client = Client(account_sid, auth_token)
            client.messages.create(from_=_from,
                                  to=to,
                                  body='Your OTP is -'+str(otp))

template.html

<form method="post">
{%csrf_token%}
    <input type="text" name="to">
    <input type="text" name="from">
</form>