【发布时间】:2014-07-29 19:53:32
【问题描述】:
我正在构建一个允许成员在文章上发布 cmets 的新闻应用程序。我想在我的模板中显示文章,并显示在每篇文章上制作的 cmets 数量。我尝试使用 count 方法,但它检索到我的 cmets 表中的 cmets 总数,而不是特定文章的 cmets 数。
#models.py
class Article(models.Model):
#auto-generate indices for our options
ENTRY_STATUS = enumerate(('no', 'yes'))
#this will be a foreign key once account app is built
author = models.CharField(default=1, max_length=1)
category = models.ForeignKey(Category)
title = models.CharField(max_length=50)
entry = models.TextField()
dateposted = models.DateTimeField(default=timezone.now, auto_now_add=True)
draft = models.IntegerField(choices=ENTRY_STATUS, default=0)
lastupdated = models.DateTimeField(default=timezone.now, auto_now=True)
#prevents the generic labeling of our class as 'Classname object'
def __unicode__(self):
return self.title
class Comment(models.Model):
#this will be a foreign key once account app is built
author = models.CharField(default=1, max_length=1)
article = models.ForeignKey(Article)
dateposted = models.DateTimeField(auto_now_add=True)
comment = models.TextField()
def __unicode__(self):
#returns the dateposted as a unicode string
return unicode(self.dateposted)
#templates/list_articles.html
{% for comment in comments %}
{% if comment.article_id == article.id %}
{% if comments.count < 2 %}
#this is returning all comments in comment table
<b>{{ comments.count }} comment</b>
{% else %}
<b>{{ comments.count }} comments</b>
{% endif %}
{% endif %}
{% endfor %}
到目前为止,我看到的所有示例都手动提供了一个过滤值(例如 Comment.objects.filter(article_id=x).count() )在我的情况下,我只能通过模板访问。
#views.py
class ArticlesListView(ListView):
context_object_name = 'articles'
# only display published pieces (limit 5)
queryset = Article.objects.select_related().order_by('-dateposted').filter(draft=0)[:5]
template_name = 'news/list_articles.html'
# overide this to pass additional context to templates
def get_context_data(self, **kwargs):
context = super(ArticlesListView, self).get_context_data(**kwargs)
#get all comments
context['comments'] = Comment.objects.order_by('-dateposted')
#get all article photos
context['photos'] = Photo.objects.all()
#context['total_comments'] = Comment.objects.filter(article_id=Article)
return context
我的预期结果是列出所有文章并在每篇文章下方对该文章进行 cmets 汇总(例如,第 1 条:4 条 cmets,第 5 条:1 条评论,等等...)现在我得到:第 1 条:4 cmets,第 5 条:4 cmets(尽管第 5 条只有 1 条评论)
感谢任何帮助。我花了 5 个小时阅读文档,但每个示例都手动提供了一个过滤值。
【问题讨论】:
标签: django django-templates django-views django-class-based-views