Python 如何使用 FormFields 的 WTForms FieldList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30121763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a WTForms FieldList of FormFields?
提问by kramer65
I'm building a website using Flaskin which I use WTForms. In a Form I now want to use a FieldList of FormFields as follows:
我正在使用Flask构建一个网站,其中使用WTForms。在表单中,我现在想使用 FormFields 的 FieldList,如下所示:
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.
我可以使用 locationForm 打印第一个位置的值(见下文),但我仍然不知道如何获取第二个位置的数据。
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?
- 为什么位置列表只有一个,而不是两个字典?
- 为什么 location dict 中的值是空的?
Does anybody know what I'm doing wrong here? All tips are welcome!
有谁知道我在这里做错了什么?欢迎所有提示!
采纳答案by Jaime Gómez
For starters, there's an argument for the FieldListcalled min_entries
, that will make space for your data:
首先,有一个FieldList的参数叫做min_entries
,它将为您的数据腾出空间:
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:
这将按照您需要的方式设置列表。接下来,您应该直接从locations
属性呈现字段,以便正确生成名称:
<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.
查看渲染的 html,输入的名称应该类似于locations-0-city
,这样 WTForms 就会知道哪个是哪个。
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.)
(仅在 wtforms 中l.city
是 的简写l.form.city
。但是,该语法似乎与 Jinja 冲突,因此有必要l.form.city
在模板中使用显式。)
Now to ready the submitted data, just create the CompanyForm
and iterate over the locations:
现在准备提交的数据,只需创建CompanyForm
并迭代位置:
for entry in form.locations.entries:
print entry.data['location_id']
print entry.data['city']