首页 > 解决方案 > 如何在 Django 中使用重组标签

问题描述

我的结构与文档建议的结构相同,但没有打印出来

<li>None
 <ul>
  <li>: </li>
  <li>: </li>
  <li>: </li>
 </ul>
</li>

打印为 920 个空白项目,这是我的结构

[{'where': 'Mumbai', 'attribute': 'title', 'error': 'India'}, {'where': 'Calcutta', 'attribute': 'nameFile', 'error': 'India '}, {' where ':' New York ',' attribute ':' sizeFile ',' error ':' USA '}, {' where ':' New York ',' attribute ':' amount_stars ',' error ': '12'}, {'where': 'Chicago', 'attribute': 'subject', 'error': 'USA'}, {'where': 'Tokyo', 'attribute': 'nameGrade', 'error': 'Japan'}, {'where': 'Mumbai', 'attribute': 'nameLevel', 'error': 'Level One'}, {'where': 'Mumbai', 'attribute': ' amount_stars', 'error': '1'}]

尝试使其与我想按位置分组的差异相同

{% regroup list_errorsProcess by where as where_list%}
<ul>
{% for where in where_list%}
                            <li> {{where.grouper}}
                                <ul>
                                    {% for err in where.list%}
                                    <li> {{err.attribute}}: {{err.error}} </li>
                                    {% endfor%}
                                </ul>
                            </li>
                        {% endfor%}
                    </ul>

突然忘记了一些东西(也许你不关心视图中的某些东西或者......)

我还需要检查元素的数量,但标签 | 长度告诉我有 462 而不是 8 .count 标签而是什么都没有,谢谢任何建议

预期结果

<ul>
    <li>Mumbai
        <ul>
            <li>title: India</li>
            <li>nameLevel: Level One</li>
            <li>amount_stars: 1</li>
        </ul>
    </li>
    <li>Calcutta
        <ul>
            <li>nameFile: India</li>
        </ul>
    </li>
    <li>New York
        <ul>
            <li>sizeFile: USA</li>
            <li>amount_stars: 12</li>
        </ul>
    </li>
    <li>Chicago
        <ul>
            <li>subject: USA</li>
        </ul>
    </li>
    <li>Tokyo
        <ul>
            <li>nameGrade: Japan</li>
        </ul>
    </li>
</ul>

标签: django

解决方案


似乎您有名称冲突,请尝试更改wheregroup

{% regroup list_errorsProcess by where as where_list %}

<ul>
    {% for group in where_list %}
        <li> {{ group.grouper}}
            <ul>
                {% for err in group.list %}
                <li> {{err.attribute}}: {{err.error}} </li>
                {% endfor%}
            </ul>
        </li>
    {% endfor%}
</ul>


推荐阅读