0

How do you create a variable sized form in Flask?

Here is my forms.py:

from flask_wtf import Form
from wtforms import StringField, BooleanField, SelectField, TextField
from wtforms.validators import DataRequired

class TestForm(Form):
    blanks = .... 

    test = []
    foreach blank in blanks
        test.append(TextField(blank, [validators.Length(min=5, max=70)]))

Here is my template:

<form action="" method="post" name="test">
  {{ form.hidden_tag() }}
  {% for test in form.tests %}
    {{ test }}
  {% endfor %}
  <p><input type="submit" value="Test"></p>
</form>

It displays "TextField..." instead of displaying the actual HTML of the form input. What am I doing wrong? How should I be doing this correctly?

Dan
  • 240
  • 5
  • 16

1 Answers1

0

First of all you named your variable in the form class test but loop over tests in the template, but thats probably just a typo.

The core problem is, that you are creating a list as class attribute, but the form you access in the template is an object of the class and not the class itself. To set one class attribute per form (that's the way wtform will generate the fields for you) you have to modify the class after you imported it. For example wit setattr() like in this answer on stackoverflow.

Lukr
  • 659
  • 3
  • 14