How to access multidimensional dictionary on Django template

时光总嘲笑我的痴心妄想 提交于 2019-12-10 11:34:09

问题


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


回答1:


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).



来源:https://stackoverflow.com/questions/12860022/how-to-access-multidimensional-dictionary-on-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!