电话验证后保存用户详细信息



我的项目中有电话验证。它将 otp 发送给用户,他们需要验证。发送并验证 otp 后,它应将用户数据保存在数据库中。但是我在验证他们的手机之前正在保存,我不知道该怎么做。有人可以帮忙吗?

这是我的代码

views.py

class RegisterApi(views.APIView):
permission_classes = (AllowAny,)
def post(self, request, *args, **kwargs):
phone_number = request.data.get('phone', False)
first_name = request.data.get('first_name', False)
last_name = request.data.get('last_name', False)
password = request.data.get('password', False)
if phone_number and first_name and last_name and password:
phone = str(phone_number)
user = User.objects.filter(phone__iexact=phone)
if user.exists():
return Response({
'status': 'Fail',
'detail': 'Phone number is already existed'
})
else:
key = send_otp(phone)
if key:
old = OTPModel.objects.filter(phone__iexact=phone)
if old.exists():
old = old.first()
count = old.count
if count > 5:
return Response({
'status': 'Fail',
'detail': 'Limit Exceeded'
})
old.count = count + 1
old.save()
return Response({
'status': 'True',
'detail': 'OTP resent',
'response': response
})

else:
OTPModel.objects.create(
phone=phone,
otp=key
)

serializer = UserSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response({
'status': 'True',
'detail': 'OTP sent successfully',
'account': serializer.data
})
else:
return Response({
'status': 'Fail',
'detail': 'Error in sending otp'
})
else:
return Response({
'status': False,
'detail': 'Phone number is not entered'
})

要验证 otp,请使用此代码。

class ValidateOTP(views.APIView):
def post(self, request, *args, **kwargs):
phone = request.data.get('phone', False)
otp_sent = request.data.get('otp', False)
if phone and otp_sent:
old = OTPModel.objects.filter(phone__iexact=phone)
if old.exists():
old = old.first()
otp = old.otp
if str(otp_sent) == str(otp):
old.verified = True
old.save()
return Response({
'status': 'True',
'detail': 'OTP verified successfully'
})
else:
return Response({
'status': 'False',
'detail': 'OTP is incorrect'
})

一切正常,但它会在验证之前保存用户详细信息。我需要在验证后保存。如何解决此问题?事先谢谢!

一旦你完成了你的if语句,你就会回来。也许可以:

if condition:
store details
return

而不是:

if condition:
return

相关内容

最新更新