How to use a WTForms FieldList of FormFields?

2020-01-29 05:35发布

问题:

I'm building a website using Flask in which I use WTForms. In a Form I now want to use a FieldList of FormFields as follows:

class LocationForm(Form):
    location_id = StringField('location_id')
    city = StringField('city')

class CompanyForm(Form):
    company_name = StringField('company_name')
    locations = FieldList(FormField(LocationForm))

so to give people the ability to enter a company with two locations (dynamic adding of locations comes later) I do this on the front side:

<form action="" method="post" role="form">
    {{ companyForm.hidden_tag() }}
    {{ companyForm.company_name() }}
    {{ locationForm.location_id() }}
    {{ locationForm.city() }}
    {{ locationForm.location_id() }}
    {{ locationForm.city() }}
    <input type="submit" value="Submit!" />
</form>

So on submit I print the locations:

print companyForm.locations.data

but I get

[{'location_id': u'', 'city': u''}]

I can print the values of the first location using the locationForm (see below), but I still don't know how to get the data of the second location.

print locationForm.location_id.data
print locationForm.city.data

So the list of locations does have one dict with empty values, but:

  1. Why does the list of locations have only one, and not two dicts?
  2. And why are the values in the location dict empty?

Does anybody know what I'm doing wrong here? All tips are welcome!

回答1:

For starters, there's an argument for the FieldList called min_entries, that will make space for your data:

class CompanyForm(Form):
    company_name = StringField('company_name')
    locations = FieldList(FormField(LocationForm), min_entries=2)

This will setup the list the way you need. Next you should render the fields directly from the locations property, so names are generated correctly:

<form action="" method="post" role="form">
    {{ companyForm.hidden_tag() }}
    {{ companyForm.company_name() }}
    {{ companyForm.locations() }}
    <input type="submit" value="Submit!" />
</form>

Look at the rendered html, the inputs should have names like locations-0-city, this way WTForms will know which is which.

Alternatively, for custom rendering of elements do

{% for l in companyForms.locations %}
{{ l.form.city }}
{% endfor %}

(in wtforms alone l.city is shorthand for l.form.city. However, that syntax seems to clash with Jinja, and there it is necessary to use the explicit l.form.city in the template.)

Now to ready the submitted data, just create the CompanyForm and iterate over the locations:

for entry in form.locations.entries:
    print entry.data['location_id']
    print entry.data['city']