Click here to Skip to main content
15,356,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys I am trying to make a comment form in django but the problem I am facing is that while posting that comment ,It gets posted on every other post .. I want it to display specific to the post where the user will comment as a normal comment form works.

Here's the
def BlogDetail(request,pk):

    post = get_object_or_404(Post,pk = pk) 
    comment_view = Comment.objects.all()
    comment = CommentForm()

    if request.method  == 'POST':
        subscribe = Subscribe(request.POST)	
        form = CommentForm(request.POST)

        if form.is_valid():
            form = = False)
            return redirect('blog',pk =

        elif subscribe.is_valid():
	    subscribe = = True)

        form = CommentForm()

    return render(request,'app/blog.html',{'blog_object':post,'comment':comment,
Here,s the html code
{% for i in comment_view %}
                {% endfor %}
Here's the
class Post(models.Model):
    image = models.ImageField()
    title = models.CharField(max_length = 100)
    body = RichTextField(blank = True)
    published_date = models.DateTimeField(auto_now_add = True)
    categories = models.ManyToManyField(Category)
    featured = models.BooleanField(default = False)

    def __str__(self):
        return self.hastags

    def __str__(self):
        return self.title

class Comment(models.Model):
    name = models.CharField(max_length = 200)
    body = models.TextField()

    def __str__(self):

What I have tried:

I stackoverflowed but no one gave the ight answer aslo i tried some answer from youtube but no one worked.
Updated 25-Feb-19 3:49am
Richard Deeming 26-Feb-19 10:27am
I don't know Django, but Comment.objects.all() suggests that you're retrieving all comments, rather than just the comments for the current post. I'd suggest looking at the documentation[^] to see how to filter that list, or use a relationship if there is one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900