Nested For Loop in Jinja2

Issue

I am making a nested for loop in Jinja / Python Flask.

If I hard code the values, then it works fine. Am I missing something in the Jinja template?

<table class="table table-striped">
<tr>
    {% for column in Columns %}
        <td>{{ column }}</td>
    {% endfor %}
</tr>

       {% for row in rows %}
           <tr>
                {% for column in Columns %}
                    <td>{{ row.column }}</td>
                {% endfor %}
           </tr>
       {% endfor %}

</table>

By the way, the output is nothing when it is not hardcoded.

Solution

Figured it out…

<table class="table table-striped">
<tr>
    {% for column in Columns %}
        <td>{{ column }}</td>
    {% endfor %}
</tr>

       {% for row in rows %}
           <tr>
                {% for column in Columns %}
                    <td>{{ row[column] }}</td>
                {% endfor %}
           </tr>
       {% endfor %}

</table>

The only changed needed was to change {{ row.column }} to {{ row[column] }} on line 11.

Answered By – Rupert

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published