Skip to content Skip to sidebar Skip to footer

Django Template In Nested Dictionary

I am using Django template, and I met one problem with nested dictionary. Dict: result_dict = {'type_0' : {'file_name' : 'abc', 'count' : 0}, 'type_1' : {'file_name'

Solution 1:

Use dict.items or dict.values:

{% for key, value in result_dict.items %}
    {{ value }}
{% endfor %}

Example in interactive shell:

>>>result_dict = {'type_0' : {'file_name' : 'abc', 'count' : 0},...'type_1' : {'file_name' : 'xyz', 'count' : 50}}>>>>>>t = Template('''...{% for key, value in result_dict.items %}...    {{ value }}...{% endfor %}...''')>>>print(t.render(Context({'result_dict': result_dict})))


    {'count': 50, 'file_name': 'xyz'}

    {'count': 0, 'file_name': 'abc'}

>>> t = Template('''
... {% for key, value in result_dict.items %}
...     {{ value|safe }}
... {% endfor %}
... ''')
>>> print(t.render(Context({'result_dict': result_dict})))


    {'count': 50, 'file_name': 'xyz'}

    {'count': 0, 'file_name': 'abc'}

Post a Comment for "Django Template In Nested Dictionary"