问题
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