构建Django 1.8应用程序,就像一个照片库(我的画廊被称为'喷泉')。我有一个包含喷泉列表的页面,我想显示喷泉名称旁边的照片数量,例如:
Fountain 1 (49 photos)
Fountain 2 (43 photos)
etc
据我所知,Django有注释来聚合和计算我用来创建以下内容的记录:
photo_count = Fountains.objects.all().annotate(Count('photos'))
然而,我无法确定的是放在哪里。文档只显示查询,但不会将查询和结果计数放在应用程序的上下文中。
我已经完成了这个' photo_count'视图中的聚合,用于为喷泉列表创建数据。
但我的问题是,如何将其插入到模板中,以便它可以显示在喷泉名称旁边?
我想我必须将它加载到模板中,然后将它与Fountain pk相关联?
任何关于Django方法如何将这种聚合从视图到模板的指针都将非常感激!
我的模特:
class Fountains(models.Model):
foun_name = models.CharField(max_length=100, blank=True, null=True)
foun_desc = models.TextField(blank=True, null=True)
foun_address = models.CharField(max_length = 100, blank=True, null=True)
foun_lat = models.CharField(max_length=20, blank=True, null=True)
foun_lon = models.CharField(max_length=20, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
#tags = TaggableManager()
#def __unicode__(self):
# return self.foun_name.name
class Meta:
managed = True
db_table = 'fountains'
#verbose_name_plural = 'Fountains'
class Photos(models.Model):
#filename = models.CharField(max_length=100)
filename = models.ImageField(upload_to='.')
ext = models.CharField(max_length=5, blank=True, null=True)
desc = models.TextField(blank=True, null=True)
#fountain_id = models.IntegerField(blank=True, null=True)
fountain = models.ForeignKey(Fountains)
user_id = models.IntegerField(blank=True, null=True)
tag_count = models.IntegerField(blank=True, null=True)
photos_tag_count = models.IntegerField(blank=True, null=True)
comment_count = models.IntegerField(blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
#def __unicode__(self):
# return self.filename.name
class Meta:
managed = True
db_table = 'photos'
#verbose_name_plural = 'Photos'
我的视图创建了喷泉列表:
@login_required
def fountains(request):
assert isinstance(request, HttpRequest)
fountain_list = Fountains.objects.order_by('foun_name')
photo_count = Fountains.objects.all().annotate(Count('photos'))
context_dict = {'fountains': fountain_list }
return render(
request,
'app/fountains.html',
context_dict,
context_instance = RequestContext(request)
)
我的模板显示视图结果:
{% extends "app/layout.html" %}
{% block content %}
<h1>Fountains</h1>
<p><a href="/fountain_add/">Add Fountain</a></p>
<ul>
{% for fountain in fountains %}
<li><a href="/photos/{{ fountain.id }}/"}>{{ fountain.foun_name }}</a></li>
{% endfor%}
</ul>
{% endblock content %}
答案 0 :(得分:1)
fountain_list = Fountains.objects.annotate(Count('photos')).order_by('foun_name')
<li><a href="/photos/{{ fountain.id }}/"}>{{ fountain.foun_name }} ({{ fountain.photos__count }})</a></li>
答案 1 :(得分:1)
<h1>Fountains</h1>
<p><a href="/fountain_add/">Add Fountain</a></p>
<ul>
{% for fountain in fountains %}
<li><a href="/photos/{{ fountain.id }}/"}>{{ fountain.foun_name }} ( {{ fountain.photos.all.count }} )</a></li>
{% endfor%}
</ul>
答案 2 :(得分:0)
在您创建的photo_count
中,您应该放置context_dict = {'fountains': fountain_list, 'photo_count': photo_count}
。
foun_
然后您可以照常在模板中使用它。
此外,在不了解您的系统如何工作的情况下,我会重命名Fountain
模型中属性的所有Test
前缀。