按循环中的字典值分组

时间:2017-02-05 09:30:36

标签: python django

我试图按照django模板中的循环字典值进行分组。

我的模板如下:

<div id="engines-rows">
    {% for engine in engines %}
        <h3>{{ engine }}</h3>
    {% endfor %}
</div>

我得到的结果如下:

{'id': 8, 'name': '2.0 TFSI', 'fuel_type': 'PETROL', 'displacement_cc': 1984, 'power_hp': 188, 'power_ps': 190, 'power_kw': 140}
{'id': 7, 'name': '2.0 TDI', 'fuel_type': 'DIESEL', 'displacement_cc': 1968, 'power_hp': 181, 'power_ps': 184, 'power_kw': 135}
{'id': 6, 'name': '2.0 TDI', 'fuel_type': 'DIESEL', 'displacement_cc': 1968, 'power_hp': 148, 'power_ps': 150, 'power_kw': 110}
{'id': 5, 'name': '1.6 TDI', 'fuel_type': 'DIESEL', 'displacement_cc': 1598, 'power_hp': 109, 'power_ps': 110, 'power_kw': 81}
{'id': 4, 'name': '1.4 TFSI g-tron', 'fuel_type': 'CNG', 'displacement_cc': 1395, 'power_hp': 109, 'power_ps': 110, 'power_kw': 81}
{'id': 3, 'name': '1.4 TFSI e-tron', 'fuel_type': 'ELECTRICITY', 'displacement_cc': 1395, 'power_hp': 148, 'power_ps': 150, 'power_kw': 110}
{'id': 2, 'name': '1.4 TFSI', 'fuel_type': 'PETROL', 'displacement_cc': 1395, 'power_hp': 148, 'power_ps': 150, 'power_kw': 110}
{'id': 1, 'name': '1.0 TFSI', 'fuel_type': 'PETROL', 'displacement_cc': 999, 'power_hp': 114, 'power_ps': 116, 'power_kw': 85}

我想按fuel_type分组,因此我希望看到类似的内容:

汽油

名称:2.0 TFSI - displacement_cc:1984 - power_hp:188 - power_ps:190 - power_kw:140 名称:1.4 TFSI - displacement_cc:1395 - power_hp:148 - power_ps:150 - power_kw:110 ........

柴油

名称:2.0 TDI - displacement_cc:1968 - power_hp:181 - power_ps:184 - power_kw:135 名称:2.0 TDI - displacement_cc:1968 - power_hp:148 - power_ps:150 - power_kw:110 ....

............

我不能这样做

<div id="engines-rows">
    {% for engine in engines %}
        {% if engine.fuel_type == "PETROL" %}
            # Do something
        {% endif %}
        {% if engine.fuel_type == "DIESEL" %}
            # Do something
        {% endif %}
         # And so on
    {% endfor %}
</div>

但是我可以动态地做,我不想为每种燃料类型添加。

由于

3 个答案:

答案 0 :(得分:1)

我考虑在您的视图中准备数据。

from collections import defaultdict

grouped_engines = defaultdict(list)

for engine in engines:
    grouped_engines[engine['fuel_type']].append(engine)

现在您的模板可以更简单。

基本结构如下:

{% for fuel_type, engines in grouped_engines.items %}
    {{ fuel_type }}
    {% for engine in engines %}
      {{ engine }}
    {% endfor %}
{% endfor %}

regroup标签应该也可以正常工作,但是将逻辑放在视图中会让人感觉更接近Django关于模板和逻辑的一般态度:

  

Data should be calculated in views, then passed to templates for display.

答案 1 :(得分:0)

只需发布Ilya V. Schurov评论中提到的样本

<div id="engines-rows">
    {% regroup engines by fuel_type as fuel_list %}
    {% for fuel in fuel_list %}
        <h2>{{ fuel.grouper }}</h2>
        {% for engine in fuel.list %}
            <h3>{{ engine }}</h3>
        {% endfor %}
    {% endfor %}
</div>

答案 2 :(得分:0)

我解决了它如下:

<div id="engines-rows">
    {% regroup engines|dictsort:"fuel_type" by fuel_type as fuel_type_list %}
    {% for fuel_type in fuel_type_list %}
        <div style="margin-bottom: 15px;">
            <div style="margin-bottom: 5px;background-color: #263238; color: #ffffff;height: 40px;display: block;padding: 10px 15px;border: 1px solid #ddd;">{{ fuel_type.grouper }}</div>
            {% for engine in fuel_type.list %}
                <a id="engine-row" class="col-lg-12" style="margin-bottom: 5px;" data-engine-id={{ engine.id }}>
                    <div class="col-lg-3" data-engine-id={{ engine.id }}>{{ engine.name }}</div>
                    <div class="col-lg-3" data-engine-id={{ engine.id }}>{{ engine.displacement_cc }} ccm</div>
                    <div class="col-lg-3" data-engine-id={{ engine.id }}>{{ engine.power_ps }} pk</div>
                    <div class="col-lg-3" data-engine-id={{ engine.id }}>{{ engine.power_kw }} kW</div>
                </a>
                <div class="clearfix" />
            {% endfor %}
        </div>
    {% endfor %}
</div>

YK Li的解决方案给了我:

<强>汽油

名称:2.0 TFSI - displacement_cc:1984 - power_hp:188 - power_ps:190 - power_kw:140

<强>柴油

名称:2.0 TDI - displacement_cc:1984 - power_hp:188 - power_ps:190 - power_kw:140 名称:2.0 TDI - displacement_cc:1984 - power_hp:188 - power_ps:190 - power_kw:140 名称:1.6 TDI - displacement_cc:1984 - power_hp:188 - power_ps:190 - power_kw:140

<强> CNG

名称:1.4 TFSI g-tron - displacement_cc:1984 - power_hp:188 - power_ps:190 - power_kw:140

<强>电力

名称:1.4 TFSI e-tron - displacement_cc:1984 - power_hp:188 - power_ps:190 - power_kw:140

<强>汽油

名称:1.4 TFSI - displacement_cc:1984 - power_hp:188 - power_ps:190 - power_kw:140 名称:1.0 TFSI - displacement_cc:1984 - power_hp:188 - power_ps:190 - power_kw:140

这不是我想要的。

In the docs (the link that Ilya V. Schurov posted)认为解决方案是使用dictsort过滤器对模板中的数据进行排序,如果您的数据位于词典列表中: