英文:
Reference comment count on different using Django
问题
我有一个BlogPage,其中引用了所有博客的摘录。这些摘录可以点击,以在ViewBlog页面上查看完整的博客,在view-blog页面底部可以添加评论,并且所有评论随后显示在此页面。
我想能够在博客页面的摘录框中引用每篇帖子上的评论数量,如下面的截图所示(我不需要帮助编写代码,只需能够在BlogPage上引用它的代码;
![enter image description here][1]
MODELS.PY
class BlogPost(models.Model):
# ... (之前的代码)
class BlogComment(models.Model):
# ... (之前的代码)
VIEWS
def BlogPage(request):
# ... (之前的代码)
def ViewBlog(request, slug):
# ... (之前的代码)
英文:
I have a BlogPage that references all my blogs with snippets of text. These can be clicked to view the full blog on a ViewBlog page, at the bottom of the view-blog page you can add a comment, and all comments are subsequently shown on this page.
I want to be able to reference the amount of comments made on every post some where on the snippet box on the blog page as screenshot below shows (I don't require help with the code for this, only the code to be able to reference it on the BlogPage;
MODELS.PY
class BlogPost(models.Model):
title = models.CharField(max_length=100, null=False, blank=False, default="")
text = RichTextUploadingField(null=True, blank=True, default="text")
featured_text = models.TextField(max_length=550, null=True, blank=True, default="text")
image = models.ImageField(null=True, blank=True, upload_to="images", default="default.png")
date = models.DateField(auto_now_add=True)
published = models.BooleanField(default=False)
featured = models.BooleanField(default=False)
slug = models.SlugField()
def save(self, *args, **kwargs):
self.slug = self.slug or slugify(self.title)
super().save(*args, **kwargs)
def __str__(self):
return self.title
class BlogComment(models.Model):
post = models.ForeignKey(BlogPost, related_name="comments", on_delete=models.CASCADE)
name = models.CharField('Name',max_length=100, default="")
text = models.TextField('Comment',max_length=1000, default="")
date = models.DateTimeField(auto_now_add=True)
status = models.BooleanField(default=True)
class Meta:
ordering = ("date",)
def __str__(self):
return '%s -- Name: %s'%(self.post.title, self.name)
VIEWS
def BlogPage(request):
posts = BlogPost.objects.filter(date__lte=timezone.now()).order_by('-date')
blog_paginator = Paginator(posts, per_page=4)
page_number = request.GET.get('page')
page = blog_paginator.get_page(page_number)
context = {'page': page}
return render(request, 'mhpapp/blog.html', context)
def ViewBlog(request, slug):
try:
blog = BlogPost.objects.get(slug=slug)
except BlogPost.DoesNotExist:
print("Blog with this slug does not exist")
blog = None
comments = blog.comments.filter(status=True)
user_comment = None
if request.method == 'POST':
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
user_comment = comment_form.save(commit=False)
user_comment.post = blog
user_comment.save()
else:
comment_form = CommentForm()
return render(request, 'mhpapp/view-blog.html', {'blog': blog, 'slug': slug, 'comments': user_comment, 'comments': comments, 'comment_form': comment_form})
答案1
得分: 0
你可以使用从评论到博客的反向关系,比如 blog.comments.all().count()
将返回与博客相关的所有评论。你可能可以在你的博客模型上使用一个方法:
@property
def comment_count(self):
return blog.comments.all().count()
然后在你的模板中,你可以使用这个属性来显示评论的数量。
英文:
You can use the reverse relation from Comment to Blog like blog.comments.all().count()
will return all comments related to a blog. You could probably use a method on your Blog model:
@property
def comment_count(self):
return blog.comments.all().count()
And in your template you can then use this property to show the amount of comments.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论