1π
β
The regroup
built in filter can do this for you without annotating your objects in the view. As the documentation says, itβs kind of complicated.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup
{% regroup all_publications by date.year as year_list %}
{% for year in year_list %}
<h1>{{ year.grouper }}</h1>
{% for publication in year.list %}
<li>{{ publication.title }}</li>
{% endfor %}
{% endfor %}
π€Peter DeGlopper
1π
I think you want the regroup
template tag;
{% regroup all_publications by date as publication_groups %}
<ul>
{% for publication_group in publication_groups %}
<li>{{ publication_group.grouper }}
<ul>
{% for publication in publication_group.list %}
<li>{{ publication.title }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
π€Timmy O'Mahony
1π
Maybe the template tag regroup
could help.
Alternatively, you could do this grouping by year in the view function (will try to provide code later).
π€stellarchariot
- [Django]-How can I use Celery in Django with just the DB?
- [Django]-POST API response blocked by CORS policy β React and Django Rest Framwork
- [Django]-Django Relation between two models
Source:stackexchange.com