Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 (‘127.0.0.1’, 55445)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

views

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 = comment_form.save(commit=False)
            # Assign the current post to the comment
            user_comment.post = post
            # Assign comment to user
            user_comment.user = request.user
            # Save the comment to the database
            user_comment.save()
        else:
            # You may include extra info here from `comment_form.errors`
            messages.error("Failed to post comment")


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

html

<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>{{comment.count}}</p>
            <p id="comment-by">Comment from <strong>{{ comment.user}}</strong> on <strong><i>{{ comment.date }}</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 %}
</div>
{% 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 %} 
    {{comment_form|crispy}}
    <button class="btn btn-outline-secondary rounded-0 comment-button" id="form-submit" type="submit" onClick="window.location.reload();">Submit</button>  
</form>

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading