如题所述
我觉得楼上的回答好像有点问题。大家可以自行验证一下我是否正确。
用get查询的时候,查询不到内容的时候会抛出异常,同样查询结果多余1条的时候也会抛出异常。因此,不能使用get,而应该使用filter。
filer若是查询不到数据,会返回一个空的查询集,[] type类型是:Queryset。
查询到多余一条的时候会,还是会返回一个包含多个对象的查询集。
所有用filter查询到的是否为'[ ]'来判断是否存在。
userinfo = Users.objects.filter(email = request.POST['email'])if userinfo.exists():
print("yes,we have this email")
else:
print("sorry,email is not register")
另外还可以使用count()这个方法,userinfo.count() =0的话表示不存在数据,大于0的话表示存在一条或多条。
还有就是楼上的答案:if uerinfo:..........else: .......
温馨提示:答案为网友推荐,仅供参考
第1个回答 2017-08-13
用if语句判断一下就可以了,比如
查找users表中是否存在email为接收到email的记录
userinfo = Users.objects.get(email = request.POST['email'])if userinfo:
print("yes,we have this email")
else:
print("sorry,email is not register")本回答被提问者采纳