[Fixed]-Rendering ordered dic in django template

1👍

Suppose you have a dict (or an Ordered one) like this:

h = {
    'a': {
        'aa': 123,
        'bb': 456,
    },
    'b': {
        'aa': 789,
        'bb': 120,
    },
    'c': {
        'aa': 345,
        'bb': 678,
    },
}

Then in the template you can do this:

{% for key, value in h.items %}
   Main key is: {{ key }}
   {% for k, v in value.items %}
        <tr><td> {{ k }}: {{ v }} </td></tr>
   {% endfor %}
{% endfor %}

Will render this:

Main key is: b
aa: 789
bb: 120

Main key is: c
aa: 345
bb: 678

Main key is: a
aa: 123
bb: 456
👤nik_m

Leave a comment