所以我有两个表, 发布 和 类别
代码:
model.py
class Category(models.Model):
category = models.CharField(max_length=100)
def __str__(self):
return self.category
class Post(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=40)
category = models.ForeignKey(Category)
content = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
class Meta:
ordering = ['-created_date']
def __str__(self):
return self.title + ' - ' + str(self.created_date.date())
我想在模板中实现类别列表。例如,我有几个类别
体育(2)2-number of how many posts are within sports category
模板代码:
<h3>Categories</h3>
<ul class="nav navbar-stacked">
{% for category in categories %}
<li><a href="#">{{ category }}<span class="pull-right">(
{{ **post.category.count** }}
)</span></a></li>
{% endfor %}
</ul>
我怎样才能做到这一点?
答案 0 :(得分:3)