django - 如何在 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>
解决方案
似乎您有名称冲突,请尝试更改where
为group
{% 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>
推荐阅读
- docker - Artifactory 中的远程 Docker 存储库行为
- javascript - 在聊天系统中跟踪消息
- ubuntu - 无法在 ubuntu 上运行 Vulkan
- json - 使用邮递员测试 Drools DRL
- gitlab - 如何邀请用户成为 GitLab 存储库的所有者?
- firebase - 如何正确配置 Flutter Firebase Database onEntryChanged 监听器?
- sharepoint - 如何按版本历史记录自动存档
- android - 在片段中的对话片段中显示谷歌地图
- mysql - 在 SQL 中过滤“\\%”
- reactjs - 如何在基于函数的组件中使用 Refs