Django通过画廊名称旁边的画廊获得照片的数量 [英] Django get count of photos by gallery beside gallery name

查看:116
本文介绍了Django通过画廊名称旁边的画廊获得照片的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构建Django 1.8应用程序,就像一个照片库(我的画廊叫做喷泉)。我有一个带有喷泉清单的页面,我想显示喷泉名称旁边的照片数量,例如:

 喷泉1( 49张照片)
喷泉2(43张照片)
etc

我明白Django已经注释了汇总和计数我用来创建以下内容的记录:

  photo_count = Fountains.objects.all() .annotate(Count('photos'))

然而,我无法确定的是将这个。该文档仅显示查询,但不将查询和结果计数在应用程序的上下文中。



我在视图中完成了这个photo_count聚合创建喷泉清单的数据。



但是我的问题是,如何让它进入模板,这样可以在喷泉名称旁边显示?



我想我必须将其加载到模板中,然后将其与喷泉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 __unicod e __(self):
#return self.foun_name.name

class Meta:
managed = True
db_table ='fountains'
#verbose_name_plural = 'Fountains'

class照片(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 = $ F $($)
user_id = models.IntegerField(blank = True,null = True)
tag_count = models.IntegerField = true,null = True)
photos_tag_count = models.IntegerField(blank = True,null = True)
comment_count = models.IntegerField(blank = True,null = True)
created = .DateTimeField(auto_now_add = True)
modified = models.DateTimeField(auto_now = True)
#def __unicode __(self )
#return self.filename.name

class Meta:
managed = True
db_table ='照片'
#verbose_name_plural ='照片'

我的观点创建了喷泉清单:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $''''''''''''''''''''''''''''''''''''''''''''' )
photo_count = Fountains.objects.all()。annotate(Count('photos'))
context_dict = {'fountains':fountain_bb
return render(
request,
'app / fountains.html',
context_dict,
context_instance = RequestContext(request)

显示视图结果的模板:

  {%extendsapp / layout.html%

{%block content%}

< h1>喷泉< / h1>
< p>< a href =/ fountain_/>添加喷泉< / a>< / p>
< ul>
{%喷泉中的喷泉%}
< li>< a href =/ photos / {{fountain.}} /}> {{fountain.Unnication}}& / A>< /锂>
{%endfor%}
< / ul>

{%endblock content%}


解决方案

$($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ < a href =/ photos / {{fountain.v}} /}> {{fountain.jpg}}({{fountain.photos.count}})< / a>< / li>


Building Django 1.8 app which is like a photo gallery (my galleries are called 'fountains'). I have a page with list of fountains and I want to show the count of photos beside the fountain name eg:

Fountain 1 (49 photos)
Fountain 2 (43 photos)
etc

I understand that Django has annotate to aggregate and count records that I have used to create the following:

 photo_count = Fountains.objects.all().annotate(Count('photos'))

However, what I cannot determine is where to put this. The documentation just shows the query but doesn't place the query and resulting counts in the context of an application.

I have done this 'photo_count' aggregation in the view that creates the data for the list of fountains.

But my question is, how to get this into the template so it can be shown beside the fountain name?

I guess i have to load it into the template and then associate it with the fountain pk?

Any pointers on what the Django methodology is to get this aggregation from view into the template would be greatly appreciated!

My models:

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'

My view that creates the list of fountains:

@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)
    )

My template that displays the view results:

{% 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 %}

解决方案

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>

这篇关于Django通过画廊名称旁边的画廊获得照片的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆