How to iterate a list in Flask

Asked

Viewed 61 times

1

I have this code in Flask (which uses the Jinja2)

{%block content%}
    {% for i in posts %}
        {% set x = x + 1 -%}
        <p style="border: 1px solid black; padding: 5px 5px; border-radius: 5px;">{{ i[x] }}</p>
    {% endfor %}
    <form action="{{ url_for('redirectposts') }}">
        <button class="btn btn-primary" type="submit">Posts</button>
    </form>
{%endblock%}
  • The x in this section starts as 0
return render_template('index.html', titulo="Home", posts=lista, x = 0)

'Posts' gets a list with some tuples.. The problem is that I can’t iterate the posts and put the tuples in the paragraphs (each tuple in a separate paragraph, going from the 1st to the last, as expected), instead it always returns a "p" like all tuples at once. How can I fix this?

1 answer

3


Good night. I think the ideal would be for you to do the following:

{%block content%}
    {% for i in posts %}
        {% for i2 in i %}
           <p style="border: 1px solid black; padding: 5px 5px; border-radius: 5px;">{{ i2 }}</p>
    {% endfor %}
    <form action="{{ url_for('redirectposts') }}">
        <button class="btn btn-primary" type="submit">Posts</button>
    </form>
{%endblock%}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.