如何显示来自 Django 的内容评论?
How do I show the comment of the content from Django?
我在 Django 上使用 PostgreSQL。
我使用的是 Django 2.2.3 和 pyton 3.7 版本。
我想显示相关内容的评论。但所有评论都出现在所有内容中。
我根据 link(https://tutorial-extensions.djangogirls.org/en/homework_create_more_models/) 中的叙述进行了编码。
我的代码:
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
单-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>```
我认为您的问题出在视图中的第二行,这个
comment = Comment.objects.filter(approved_comment=True)
您收到的所有评论的批准状态均为 True。虽然它应该是,
post = Post.objects.get(status=2, slug=slug) # notice the get instead of filter
comment = Comment.objects.filter(approved_comment=True, post=post)
因此,您过滤了与您之前在该行中获得的 post 相关的评论。
希望对您有所帮助!
我在 Django 上使用 PostgreSQL。 我使用的是 Django 2.2.3 和 pyton 3.7 版本。
我想显示相关内容的评论。但所有评论都出现在所有内容中。
我根据 link(https://tutorial-extensions.djangogirls.org/en/homework_create_more_models/) 中的叙述进行了编码。
我的代码:
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
单-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>```
我认为您的问题出在视图中的第二行,这个
comment = Comment.objects.filter(approved_comment=True)
您收到的所有评论的批准状态均为 True。虽然它应该是,
post = Post.objects.get(status=2, slug=slug) # notice the get instead of filter
comment = Comment.objects.filter(approved_comment=True, post=post)
因此,您过滤了与您之前在该行中获得的 post 相关的评论。
希望对您有所帮助!