Jinja2 Group按月/年 [英] Jinja2 Group by Month/year

查看:57
本文介绍了Jinja2 Group按月/年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按月/年对Jinja中的日期/时间列表进行分组.这是我现在拥有的代码:

I'm trying to group a list of date/times in Jinja by month/year. Here's the code 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 %}

但是问题是它当前按特定日期分组.我想按月/年(即2011年1月,2011年2月等)进行分组.

But the problem is that it currently groups by a specific date. I'd like to group by Month/Year (i.e. January 2011, February 2011 etc..).

用Python代替这样做会更有效吗?

Would it be more efficient to do this in Python instead?

谢谢!

推荐答案

您可以先进行groupby('date.year'),然后再进行groupby('date.month').

You could 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 %}

这篇关于Jinja2 Group按月/年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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