如何使用FormFields的WTForms FieldList? [英] How to use a WTForms FieldList of FormFields?
问题描述
我正在使用 Flask 构建一个网站,其中使用 WTForms 的。在表单中,我现在想要使用FormFields的FieldList,如下所示:
$ b $ $ $ $ p $ = StringField('location_id')
city = StringField('city')
$ b $ class CompanyForm(Form):
company_name = StringField('company_name')
locations = FieldList(FormField(LocationForm))
让人们有能力进入一个有两个地点的公司(位置的动态添加会在后面)我在前面做这个:
$ b
< form action =method =postrole =form>
{{companyForm.hidden_tag()}}
{{companyForm.company_name()}}
{{locationForm.location_id()}}
{{locationForm.city() }}
{{locationForm.location_id()}}
{{locationForm.city()}}
< input type =submitvalue =Submit! />
< / form>
所以在提交我打印的位置:
print companyForm.locations.data
但是我得到
[{'location_id':u'','city':u''}]
$ b我可以使用locationForm打印第一个位置的值(见下文),但是我仍然不知道如何获取数据的第二个位置。print locationForm.location_id.data
print locationForm.city.data
所以位置列表中有一个空值的字典,但是:
$ b $ ol
有人知道我在做什么错吗?所有的提示都是值得欢迎的!
对于初学者来说, FieldList ,名为 min_entries
,这将为您的数据留出空间: class CompanyForm(Form):
company_name = StringField('company_name')
locations = FieldList(FormField(LocationForm ),min_entries = 2)
这将以您需要的方式设置列表。接下来,您应该直接从 locations
属性中显示字段,以便正确生成名称:
< form action =method =postrole =form>
{{companyForm.hidden_tag()}}
{{companyForm.company_name()}}
{{companyForm.locations()}}
< / form>
查看呈现的html,输入的名称应该像 locations-0 -city
,这样WTForms就知道哪个是哪个。
另外,对于元素的自定义渲染,也可以使用
{%for companyForms.locations%}
{{l.form.city}}
{%endfor%}
(单独使用wtforms l.city
l.form.city
。但是,这个语法似乎与Jinja发生冲突,在这里有必要使用显式的 l.form.city
。)
现在准备提交的数据,只需创建 CompanyForm
并遍历位置:
用于输入form.locations.entries:
打印entry.data ['location_id ']
print entry.data ['city']
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:
- Why does the list of locations have only one, and not two dicts?
- And why are the values in the location dict empty?
Does anybody know what I'm doing wrong here? All tips are welcome!
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']
这篇关于如何使用FormFields的WTForms FieldList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!