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

标签 python django dictionary django-templates

我正在尝试访问 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/

相关文章:

javascript - 在我的ajax请求发生在django中的文本之前必须刷新页面

Django查询: WHERE field LIKE '10__8__0__' ?怎么写

python - 以编程方式使用的免费单词列表?

Python 解释器将异步操作的控制权交还给 C 调用者

python - 在 Pandas 数据框和平均数组中按列分组

python - 当我们仅从该模块调用函数时,是否会导入模块顶部的包

Django:存储所有应用程序通用功能的位置

python - 从列表转到字典键

c++ - map 插入失败 : can not overwrite first element?

python - 检查输入是否为正整数