Flask pattern - for loop iteration loop: value

I have an HTML template with Flask Jinja for a loop that creates a table and looks like this:

<tbody>
  {% for segment in segment_details %}
    <tr>
      <td>{{segment}}</td>
      <td>{{segment_details['{{segment}}']}}</td>
    </tr>
  {% endfor %}
</tbody>

      

I am trying to iterate through a variable length / keys document and represent each row in a table as a key and value. In my Python code, I have one that has the desired response in a shell:

        for item in segment_details:
            print(item, segment_details[item])

      

But in Flask I am getting the element correctly by listing all the lines, but

{{segment_details['{{segment}}']}}

Doesn't produce any values, I've tried with and without single quotes. Is it possible?

+3


source to share


2 answers


This is where your mistake is:

<td>{{segment_details['{{segment}}']}}</td>

      

{{ }}

No need inside . It should be simple:



<td>{{segment_details[segment]}}</td>

      

See the documentation for Jinja for details . When you write operator ( if

, for

) in Jinja2

, you use {% statement %}

, but when you access a variable, just use {{ variable }}

.

+1


source


this decision



<tbody>
  {% for key, segment in segment_details.items() %}
    <tr>
      <td>{{ key }}</td>
      <td>{{ segment }}</td>
    </tr>
  {% endfor %}
</tbody>

      

+1


source







All Articles