简体   繁体   中英

How to access multidimensional dictionary on Django template

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM