matching query does not exist

I am trying to add the feature of commenting and replying to it in my blog. But it is constantly throwing me the error of “BlogComment matching query does not exist.”

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")

Rather than trying to handle POST variables directly, I suggest you review the Working with forms docs page to see how to use forms to more easily handle user input.

1 Like