Skip to content
Advertisement

When i hit the “PUBLISH COMMENT BUTTON” the comments does not get posted in my frontend in django

i am making a comment section in django but when i hit the comment section the comment does get published to the comment section of my website it just refreshed the page and does nothing but when i add a comment from my backend which is the admin section it works perfectly fine and get updated in my front end but the comment form in my blog posts details doesnt work, let me show some of my code

views.py

# this view returns the blog details and the comment section with the form
def blog_detail(request, blog_slug):
    post = get_object_or_404(Blog, slug=blog_slug)
    # post = Blog.objects.filter(slug=blog_slug)
    categories = Category.objects.all()
    comments = post.comments.filter(active=True)
    new_comment = None
    if request.method == "POST":
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            new_comment.name = request.user
            new_comment.save()
    else:
        comment_form = CommentForm()

    context = {
        'post': post,
        'comments': comments,
        'comment_form': comment_form,
        'new_comment': new_comment,
        'categories': categories,
    }
    return render(request, 'blog/blog-details.html', context)

forms.py

class CommentForm(forms.ModelForm):
    # tags = forms.CharField(widget=forms.TextInput(attrs={'class': 'input is-medium'}), required=True)
    
    class Meta:
        model = Comment
        fields = ['email', 'body']

admin.py

@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ('name', 'body', 'post', 'created_on')
    list_filter = ('active', 'created_on')
    search_fields = ['approve_comment']

    def approve_comment(self, request, queryset):
        queryset.update(active=True)

models.py

class Comment(models.Model):
    post = models.ForeignKey(Blog, on_delete=models.CASCADE, related_name='comments')
    name = models.ForeignKey(User, on_delete=models.DO_NOTHING, verbose_name="Name")
    email = models.EmailField()
    body = models.TextField(verbose_name="Write Comment")
    created_on = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)

    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return 'Comment: {} by {}'.format(self.body, self.name)

blogdetail.html this template render the comments forms too

<div class="comment-form">
                                            <form action="#">
                                                <div class="row">
                                                    {% if new_comment %}
                                                    <div class="alert alert-success" role="alert">
                                                        Your comment is awating approval
                                                    </div>
                                                    {% else %}
                                                    <form method="POST">
                                                        {% csrf_token %}
                                                        {{comment_form|crispy}} <br>
                                                        <button type="submit">Post Comment</button>
                                                        
                                                    </form>
                                                    {% endif %}
                                                </div>
                                            </form>
                                        </div>

i have tried a lot of ways to fix this but it end up not working and note i do not get any error but it just refreshes the page and then no comments shows up.

any help would be greatly appreciated

Advertisement

Answer

template

<div class="comment-form">
                                            
                                                <div class="row">
                                                    {% if new_comment %}
                                                    <div class="alert alert-success" role="alert">
                                                        Your comment is awating approval
                                                    </div>
                                                    {% else %}
                                                    <form action="" method="POST">
                                                        {% csrf_token %}
                                                        {{comment_form|crispy}} <br>
                                                        <button type="submit">Post Comment</button>
                                                        
                                                    </form>
                                                    {% endif %}
                                                </div>
                                           
                                        </div>

views.py

def blog_detail(request, blog_slug):
    post = get_object_or_404(Blog, slug=blog_slug)
    # post = Blog.objects.filter(slug=blog_slug)
    categories = Category.objects.all()
    comments = post.comments.filter(active=True)
    new_comment = post.comments.filter(active=False,name=request.user)
    if request.method == "POST":
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            new_comment.name = request.user
            new_comment.active = False #this make the comment non active until it is approuved by admin
            new_comment.save()
            return redirect('the-url-of-the-blog-detail')

    else:
        comment_form = CommentForm()

    context = {
        'post': post,
        'comments': comments,
        'comment_form': comment_form,
        'new_comment': new_comment,
        'categories': categories,
    }
    return render(request, 'blog/blog-details.html', context)
Advertisement