[Answered ]-I cannot post comment in Django, no error

1👍

You need to redirect() after dealing with POST data, the tip is not specific to Django, its a good web practice in general so:

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages

def post_detail(request, slug):
    post = get_object_or_404(Post, slug=slug)
    related_posts = post.tags.similar_objects()[:3]
    
    comments = post.comments.filter(active=True)
    new_comment = None
    if request.method == 'POST':
        if request.user.is_authenticated:
            comment_form = CommentForm(data=request.POST)
            if comment_form.is_valid():
                new_comment = comment_form.save(commit=False)
                new_comment.post = post
                new_comment.author = request.user
                new_comment.save()
                return redirect(post.get_absolute_url()) # redirect here
        else:
            messages.warning(request, 'You need to be logged in to add a comment.')
    else:
        if request.user.is_authenticated:
            comment_form = CommentForm(initial={'author': request.user})
        else:
            comment_form = CommentForm()

    context = {'post': post, 'related_posts': related_posts, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form}
    return render(request, 'post_detail.html', context)

Leave a comment