Click here to Skip to main content
15,885,869 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 views.py
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 = form.save(commit = False)
            form.save()
            return redirect('blog',pk = post.pk)

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

    else:
        form = CommentForm()

    return render(request,'app/blog.html',{'blog_object':post,'comment':comment,
                                         'comment_view':comment_view})
Here,s the html code
HTML
{% for i in comment_view %}
                {{i.name}}
                {{i.body}}
                {% endfor %}
Here's the models.py
Python
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):
       return self.name


What I have tried:

I stackoverflowed but no one gave the ight answer aslo i tried some answer from youtube but no one worked.
Posted
Updated 25-Feb-19 3:49am
v2
Comments
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