I am use PostgreSQL on Django. I am use Django 2.2.3 and pyton 3.7 version.
I want to display comments for related content. But all comments appear in all content.
I made the coding based on the narration found in the link(https://tutorial-extensions.djangogirls.org/en/homework_create_more_models/).
My codes:
views.py
def PostDetail(request, slug):
post = Post.objects.filter(status=2, slug=slug)
comment = Comment.objects.filter(approved_comment=True)
comment_count = Comment.objects.count()
if request.method == 'POST':
post_id = post['id']
print('hata: ', post_id)
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.Post = post
new_comment.save()
return redirect('blog:post_detail', slug)
else:
comment_form = CommentForm()
return render(request, 'single-blog.html',
{
'post_details': post,
'comments': comment,
'comment_form': comment_form,
'comment_count': comment_count
}
)
models.py
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
updated_on = models.DateTimeField(auto_now=True)
content = HTMLField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
model_pic = models.ImageField(upload_to='uploads/', default='upload image')
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
# reply_to = models.ForeignKey('self', related_name='replies', null=True, blank=True)
author = models.CharField(max_length=200)
comment = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
comment_image = models.ImageField(upload_to='uploads/', default='upload image')
def approve(self):
self.approved_comment = True
self.save()
def __str__(self):
return self.comment
single-blog.html
<div class="comments-area">
<h4>Comments:</h4>
<div class="comment-list">
{% for comment in comments %}
<div class="single-comment justify-content-between d-flex">
<div class="user justify-content-between d-flex">
<div class="thumb">
<img src="img/blog/c1.png" alt="">
</div>
<div class="desc">
<p class="comment">{{ comment.comment|linebreaks }}</p>
<div class="d-flex justify-content-between">
<div class="d-flex align-items-center">
<h5>
<a href="#">{{ comment.author }}</a>
</h5>
<p class="date">{{ comment.created_date }}</p>
</div>```
I think your problem is with your second line in the view, this
comment = Comment.objects.filter(approved_comment=True)
You are getting all comment with approved status True. While it should be,
post = Post.objects.get(status=2, slug=slug) # notice the get instead of filter
comment = Comment.objects.filter(approved_comment=True, post=post)
So you filter on the comment related to the post you got in the line before.
Hope this helps!