Issue
I am trying to verify otp. The table in db for otp contains email and otp_no. I am getting the expected outcome if the otp provided by the user is correct but get "AttributeError: 'NoneType' object has no attribute 'email'" if the otp is wrong.
Following is the code:
@api_view(['POST'])
def verifyOtp(request):
email = request.data.get('email')
otp_no = request.data.get('otp_no')
res = dict()
if email!= None and otp_no!= None:
result = Otp.objects.filter(email=email, otp_no = otp_no).first()
if len(result.email)> 0:
res['isOtpVerified'] = True if otp_no == result.otp_no else False
return JsonResponse(res)
else:
res['emailExist'] = False
return JsonResponse(res)
what am I missing in the code?
Solution
Probably there are no entries in the database satisfying your filters. You are filtering Otp.objects.filter(email=email, otp_no = otp_no).first()
. You are checking that email != None and otp_no != None (btw more clear way to do this is by writing email is not None and otp_no is not None
). But even if they are not None user can pass the wrong email or wrong otp_no that are not exists in DB. Therefore you can have None value in your result variable, leading to that AttributeError.
Answered By - Viktor Mironov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.