首页 > 解决方案 > 如何在 django 中使用移动 otp 验证用户注册?

问题描述

我的 urls.py '''

from django.urls import path, include
from . import views
urlpatterns = [
    path('enter_otp',views.otp_call,name='otp_confirmed')    
]'''

我的意见.py

'''

def signup(request):
    if request.method == 'POST':
        username = request.POST["username"]
        last_name = request.POST["last_name"]
        first_name = request.POST["first_name"]
        date_of_birth = request.POST["birthday"]
        email = request.POST["email"]
        password = request.POST["password"]
        Phone = request.POST["phone"]
        gender = request.POST["gender"]
        if User.objects.filter(username=username).exists():
            messages.info(request,'username already taken')
            return render(request,"signup.html")
        if User.objects.filter(phone_no=Phone).exists():
            messages.error(request,'Phone number already in use')
            return render(request,"signup.html")
        else:
            sent = otp()
            print(sent)
            return otp_call(un=username,pswrd= password,mail = email, fn=first_name,ln=last_name,pn=Phone,dob=date_of_birth,gen=gender,otp=sent)
    else:
        return render(request,"signup.html")


def otp_call(request,**kwargs):
    if request.method == 'POST':
        trials = 0
        while trials<3:
            input_otp= request.POST["six_digit"]
            if input_otp == otp:
                signedup_user = User.objects.create_user(
                    username=un,
                    password=pswrd,
                    email=mail,
                    first_name=fn,
                    last_name =ln,
                    phone_no=pn,
                    date_of_birth=dob,
                    gender= gen
                )
                signedup_user.save()
                break
                return redirect('login')       
            if input_otp != otp:
                trials+=1
                messages.error(request,"wrong otp")
                return render(request,'otp_recieve.html')
    else: 
        return render(request,'otp_recieve.html')

'''

我有 html 来确认带有 post request 和 csrf_token 的 otp 但我知道这不是正确的方法,因为 Urls.py 也需要参数我如何通过那里的移动设备验证使用 otp 的用户,而 signup() 中的 otp() 是只是一个随机的 6 位密钥生成器,请帮帮我

标签: pythondjangopython-3.xdjango-views

解决方案


您可以在请求正文中将生成的 otp 作为前端的有效负载发送到后端。您可以编写一个 ajax 调用来执行此操作

然后两个从 backed 访问数据,就

import requests
otp=req['otp']

推荐阅读