![](/img/trans.png)
[英]Why do I get AttributeError: 'NoneType' object has no attribute 'attrs'?
[英]Why do I get AttributeError: 'NoneType' object has no attribute 'email'
我正在嘗試驗證 otp。 用於 otp 的 db 中的表包含 email 和 otp_no。 如果用戶提供的 otp 正確,我會得到預期的結果,但如果 otp 錯誤,則會得到“AttributeError: 'NoneType' object has no attribute 'email'”。
以下是代碼:
@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)
我在代碼中遺漏了什么?
數據庫中可能沒有滿足您過濾器的條目。 您正在過濾Otp.objects.filter(email=email, otp_no = otp_no).first()
。 您正在檢查 email != None 和 otp_no != None (順便說一句,更清晰的方法是編寫email is not None and otp_no is not None
)。 但即使他們不是 None 用戶也可以傳遞錯誤的 email 或錯誤的 otp_no 數據庫中不存在。 因此,您的結果變量中可以有 None 值,從而導致 AttributeError。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.