如何在Django模板上访问多维字典

时间:2012-10-12 13:22:12

标签: python django dictionary django-templates

我正在尝试访问Django模板中的多维字典。我能够查看第一级键,但由于二级键我看不到任何东西。在示例中,词典以这种方式组成:

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

我可以使用模型吗?或者我可以使用这本字典吗?

由于

1 个答案:

答案 0 :(得分:5)

我在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)中分解。

相关问题