Jinja2 Group for a month / year

I am trying to group a date / time list in Jinja by month / year. Here is the code that I have right now:

{% for group in EventsList|groupby('date') %} <b>{{group.grouper}}</b><br /> {% for event in group.list %} <i>{{event.title}}</i> {% endfor %} {% endfor %} 

But the problem is that at the moment it is grouped by a certain date. I would like to group by month / year (e.g. January 2011, February 2011, etc.).

Would it be more efficient to do this in Python?

thanks!

+4
source share
1 answer

You can first groupby ('date.year') and then groupby ('date.month').

 {% for year, year_group in EventsList|groupby('date.year') %} {% for month, list in year_group|groupby('date.month') %} <b>{{ month }} {{ year }}</b><br /> {% for event in list %} <i>{{event.title}}</i> {% endfor %} {% endfor %} {% endfor %} 
+6
source

Source: https://habr.com/ru/post/1438198/


All Articles