我想检查一个对象是否存在-如果它不存在,我想继续这个函数,不返回404错误。我怎样才能做到这一点?你知道吗
def check(request):
if request.is_ajax():
# print('Working') #prints
id = request.POST.get('id')
post = Post.objects.get(hash=id)
obj = get_object_or_404(Post, post=post)
if obj:
# do stuff
else:
#do something else
上述代码返回:
Not Found: /check/
[22/Jul/2018 01:15:03] "POST /check/ HTTP/1.1" 404 1729
如果您经常使用它,那么您可以将它作为函数并保存在助手.py你知道吗
把它当作
只需捕获
Post.DoesNotExist
异常并在异常处理程序中执行其他操作:相关问题 更多 >
编程相关推荐