Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

python - How to use a WTForms FieldList of FormFields?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...