Search code examples
pythondjangotwilioone-time-password

How to realize sending otp on the phone?


I am trying to implement phone registration using twilio, python and django

I can`t comprehend how i have to realize mechanism , that takes input from user for texting him

Generating otp and sending to user:

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))

When user inputs his phone number, it sends to the server

But how i can place his number at to =" _HERE_ " , when he sends it to the server and how i can call this file then?


Solution

  • In your views simply get the user input and send it to the 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>