【问题标题】:how to display number of comments in django如何在 django 中显示评论的数量
【发布时间】:2016-03-11 13:25:41
【问题描述】:

我有点困惑,我有两个模型

class Comment(models.Model):
    user = models.ForeignKey(MyProfile)
    parent = models.ForeignKey("self", null=True, blank=True)
    path = models.CharField(max_length=350)
    post = models.ForeignKey(Post, null=True, blank=True, related_name="commented_post")
    text = models.TextField()

还有

class Post(models.Model):
    #comment = models.ForeignKey(Comment)if I do this I get an error but how do I display the number of comments? I was thinking to do {{post.comment.count}}

我尝试在评论模型下添加以下功能

@property
    def get_comment_count(self):
        return self.comment.count

但这不起作用,我想我必须使用{{comment.post.count}}maybe之类的东西......请帮助......

【问题讨论】:

标签: django


【解决方案1】:

您似乎正在尝试获取特定帖子的 cmets 数量,您可以这样做 comments_count = Comment.objects.filter(post=mypost).count()

【讨论】:

  • 这进入views.py 对吧?然后我应该在我的模板中使用 cmets_count
  • 这完全没有必要......假设一个帖子对象已经在上下文中,你不需要添加任何其他东西到views.py。您可以通过...访问评论计数...{{post_object_name.commented_post.count}}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2018-10-12
  • 1970-01-01
  • 2022-08-14
  • 2019-12-07
  • 2018-02-25
  • 2022-11-18
相关资源
最近更新 更多