匹配查询不存在。 /blog/postComment 不存在
matching query does not exist. DoesNotExist at /blog/postComment
我正在尝试在我的博客中添加评论和回复功能。但它不断地向我抛出“BlogComment 匹配查询不存在”的错误。
def postComment(request):
if request.method == "POST":
comment = request.POST.get('comment')
user = request.user
postSno = request.POST.get('postSno')
post = Post.objects.get(sno=postSno)
parentSno = request.POST.get('parentSno')
if parentSno == "":
comment = BlogComment(comment=comment, user=user, post=post)
comment.save()
messages.success(request, "Your comment has been posted successfully")
else:
parent = BlogComment.objects.get(sno=parentSno)
comment = BlogComment(comment=comment, user=user, post=post, parent=parent)
comment.save()
messages.success(request, "Your reply has been posted successfully")
当指定键不存在时,get()
方法returnsNone
默认。
所以你应该检查 if parentSno is not None:
而不是 if parentSno == "":
。
(或者,您也可以使用 get()
方法的第二个参数更改该默认值:parentSno = request.POST.get('parentSno', "")
(例如 see here))。
我正在尝试在我的博客中添加评论和回复功能。但它不断地向我抛出“BlogComment 匹配查询不存在”的错误。
def postComment(request):
if request.method == "POST":
comment = request.POST.get('comment')
user = request.user
postSno = request.POST.get('postSno')
post = Post.objects.get(sno=postSno)
parentSno = request.POST.get('parentSno')
if parentSno == "":
comment = BlogComment(comment=comment, user=user, post=post)
comment.save()
messages.success(request, "Your comment has been posted successfully")
else:
parent = BlogComment.objects.get(sno=parentSno)
comment = BlogComment(comment=comment, user=user, post=post, parent=parent)
comment.save()
messages.success(request, "Your reply has been posted successfully")
当指定键不存在时,get()
方法returnsNone
默认。
所以你应该检查 if parentSno is not None:
而不是 if parentSno == "":
。
(或者,您也可以使用 get()
方法的第二个参数更改该默认值:parentSno = request.POST.get('parentSno', "")
(例如 see here))。