如何在Django模板上访问多维字典 [英] How to access multidimensional dictionary on Django template

查看:576
本文介绍了如何在Django模板上访问多维字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问Django模板中的多维字典。我能够查看一级密钥,但由于第二级密钥我看不到任何东西。在这个例子中,字典是这样组成的:

  dictionary = {} 
dictionary [first_level] = {}
字典[first_level] [second_level] = {}
...

等等

从Django模板我使用:

  {%for flk in dict%} 
<! - 从以下使用嵌套,没有显示输出 - >
{%for dict.flk%中的slk%
< th一级密钥:{{flk}}二级密钥:{{slk}}< / th>
{%endfor%}
<! - - >
{%endfor%}

有没有使用模型或可以使用这个字典?



谢谢

解决方案

这个页面
基本上代码变成

  {%for flk,flv in dict.items%} 
{%for slk,slv in flv.items%}
<第一级密钥{{flk}}第二级密钥{{slk}}< / th>
{%endfor%}
{%endfor%}

在密钥(flk,slk)和值(flv,slv)中分解


I am trying to access a multidimensional dictionary in a Django template. I am able to view first level keys, but since second level keys I cannot see anything. In example dictionary is composed in this way:

dictionary = {}
dictionary[first_level] = {}
dictionary[first_level][second_level] = {}
...

and so on

From Django template I use:

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

Have I to use a model or can I do it using this dictionary?

Thanks

解决方案

I've found the solution on this page Basically the code becomes

{% for flk, flv in dict.items %}
    {% for slk, slv in flv.items %}
        <th>First level key {{ flk }} Second level key {{ slk }}</th>
    {% endfor %}
{% endfor %}

where each dictionary is decomposed in keys (flk, slk) and values (flv, slv).

这篇关于如何在Django模板上访问多维字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆