Home > Back-end >  Iterating over a list from an object in Django template
Iterating over a list from an object in Django template

Time:07-05

I am trying to iterate over a list that is below an object in a Django template. Unfortunately I am not getting anywhere.

This is the object:

{'Value1': [8, 5, 4, 7, 4, 5], 'Value2': [], 'Value3': []}

I am doing in the template something like:

{% for entry in data %}
<tr>
    <td>{{ entry }}</td>
    <td>{{ entry.0 }}</td>
</tr>
{% endfor %}

or

{% for entry in data %}
<tr>
    <td>{{ entry }}</td>
    {% for i in entry %}
    <td>{{ i }}</td>
    {% endfor %} 
</tr>
{% endfor %}

But all I am getting is just the first letter of the key.

CodePudding user response:

You have to look for both - keys and values in dict's items:

{% for key, value_list in data.items %}
<tr>
    <td>{{ entry }}</td>
    {% for value in value_list %}
        <td>{{ value }}</td>
    {% endfor %}
</tr>
{% endfor %}

CodePudding user response:

Did you try iterating over the items in your dict, like so?

{% for key, value in data.items %}
  • Related