Skip to content Skip to sidebar Skip to footer

Jinja: Loop To Create Form Fields With Same Name But The Last Character

I am using Flask and I have a WTF form with 12 input fields named like sold_1, sold_2,..., sold_12. I would like to generate these fields in Jinja using a loop like: {% for r in ra

Solution 1:

You could use this:

{% for r in range(1, 13) %}
    {{ form.sold_ ~ r }}
{% endfor %}

or, if you want your input fields names to be sold_nr:

{% for r inrange(1, 13) %}
    {{ 'sold_' ~ r }}
{% endfor %}

See this answer for more detail.

EDIT

Using the @dirn and @Libra sugestions the correct answer is:

{% for r in range(1, 13) %}
    {{ form['sold_' ~ r] }}
{% endfor %}

Post a Comment for "Jinja: Loop To Create Form Fields With Same Name But The Last Character"