我正在尝试访问 Django 模板中的多维字典。我可以查看一级 key ,但由于二级 key 我什么也看不到。在例子中字典是这样组成的:
dictionary = {}
dictionary[first_level] = {}
dictionary[first_level][second_level] = {}
...
and so on
从我使用的 Django 模板中:
{% for flk in dict %}
<!-- Using nested for from the following, no output is shown -->
{% for slk in dict.flk %}
<th>First level key : {{ flk }} Second level key : {{ slk }}</th>
{% endfor %}
<!-- -->
{% endfor %}
我必须使用模型还是可以使用这本字典?
谢谢
最佳答案
我在 this page 上找到了解决方案 基本上代码变成了
{% for flk, flv in dict.items %}
{% for slk, slv in flv.items %}
<th>First level key {{ flk }} Second level key {{ slk }}</th>
{% endfor %}
{% endfor %}
其中每个字典都分解为键 (flk, slk)
和值 (flv, slv)
。
关于python - 如何在 Django 模板上访问多维字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12860022/