Comments not registering into database when submitting form

When I try add a comment via my form, the page is redirected correctly but no comment is rendered/added.

I am getting the following error in terminal when trying to add comment:

Broken pipe from (‘’, 55445)


def post_comment(request, slug):
    template_name = 'view-post.html'
    post = get_object_or_404(BlogPost, slug=slug)

    # Comment posted
    if request.method == 'POST':
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():

            # Create Comment object but don't save to database yet
            user_comment =
            # Assign the current post to the comment
   = post
            # Assign comment to user
            user_comment.user = request.user
            # Save the comment to the database
            # You may include extra info here from `comment_form.errors`
            messages.error("Failed to post comment")

    return redirect('viewpost', {'slug': slug,})


<div class="comments-section">
    {% with post.comments.count as total_comments %}
        <strong><u>{{ total_comments }} Comment</u></strong> {{ total_comments|pluralize }}
    {% endwith %}
    {% for comment in post.comments.all %}
        {% if comment.status %}
            <p id="comment-by">Comment from <strong>{{ comment.user}}</strong> on <strong><i>{{ }}</i></strong>
            <p>{{ comment.text}}</p>
        {% endif %}
    {% empty %}
    <p style="font-weight: bold;">No comments for the blog post yet, why not add one?</p>
    {% endfor %}
{% if user.is_authenticated %}
<button class="btn btn-outline-secondary rounded-0 comment-button" id="add">Add Comment</button>
<div id="add-comment">
<form method="POST" id="comment-form">
    {% csrf_token %} 
    <button class="btn btn-outline-secondary rounded-0 comment-button" id="form-submit" type="submit" onClick="window.location.reload();">Submit</button>  

>Solution :

You do not set any url in your form. I suppose your form is just sent to viewpost which not contains any POST process so, you display again same page.

Leave a Reply