python - 如何在 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 位密钥生成器,请帮帮我
解决方案
您可以在请求正文中将生成的 otp 作为前端的有效负载发送到后端。您可以编写一个 ajax 调用来执行此操作
然后两个从 backed 访问数据,就
import requests
otp=req['otp']
推荐阅读
- hibernate - 如何在休眠中正确映射一对一?
- python-3.x - pywinauto - 如何控制 MFCReportCtrl?
- c# - 尝试从 xml 字符串中获取 xml 节点值时,序列不包含任何元素
- javascript - 如果存在 onload,如何只加载一次 iframe?
- reactjs - 来自 Redux 的规范化数据未显示在前端(使用反应)
- php - 通过本地主机 (XAMPP) 发送电子邮件时遇到问题
- apollo - 重构和 Apollo 查询
- drupal-8 - 在块创建drupal 8上放置块
- visual-studio - ffmpeg 使用 msys2 + VS2017 构建挂起
- spring-boot - DynamoDbMetaDataStore 的动态运动检查点