Categories
Python Answers

How to iterate through dictionary in a dictionary in a Python Django template?

Spread the love

To iterate through dictionary in a dictionary in a Python Django template, we can loop through the items with a for loop.

For instance, we write

<table>
    <tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
    </tr>

    {% for key, values in data.items %}
    <tr>
        <td>{{key}}</td>
        {% for v in values[0] %}
        <td>{{v}}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>

to loop through the data dict’s entries with a for loop.

We get the key value pairs with data.items.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *