Skip to content Skip to sidebar Skip to footer

Django Get Count Of Photos By Gallery Beside Gallery Name

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

Solution 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>

Solution 2:

in your html

<h1>Fountains</h1><p><ahref="/fountain_add/">Add Fountain</a></p><ul>
    {% for fountain in fountains %}
        <li><ahref="/photos/{{ fountain.id }}/"}>{{ fountain.foun_name }} ( {{ fountain.photos.all.count }} )</a></li>
    {% endfor%}
    </ul>

Solution 3:

In the context_dict that you create is where you should put your photo_count.

context_dict = {'fountains': fountain_list, 'photo_count': photo_count}

Then you can use this in your templates as normal. Also, without any knowledge about how your system works, I would rename all of the foun_ prefixes from the properties in your Fountain model.

Post a Comment for "Django Get Count Of Photos By Gallery Beside Gallery Name"