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:
Does anybody know what I'm doing wrong here? All tips are welcome!
WTForms is a flexible forms validation and rendering library for Python web development. It can work with whatever web framework and template engine you choose. It supports data validation, CSRF protection, internationalization (I18N), and more.
StringField (default field arguments)[source]¶ This field is the base for most of the more complicated fields, and represents an <input type="text"> .
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With