如何使用 FormFields 的 WTForms FieldList? [英] How to use a WTForms FieldList of FormFields?

查看:46
本文介绍了如何使用 FormFields 的 WTForms FieldList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Flask 构建网站,其中我使用了 WTForms.在表单中,我现在想使用 FormFields 的 FieldList,如下所示:

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

但我明白

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

我可以使用 locationForm 打印第一个位置的值(见下文),但我仍然不知道如何获取第二个位置的数据.

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. 为什么位置列表只有一个而不是两个字典?
  2. 为什么 location dict 中的值是空的?

有人知道我在这里做错了什么吗?欢迎所有提示!

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

推荐答案

对于初学者来说,FieldList 称为 min_entries,这将为您的数据腾出空间:

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)

这将按照您需要的方式设置列表.接下来,您应该直接从 locations 属性呈现字段,以便正确生成名称:

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>

查看渲染的 html,输入的名称应该类似于 locations-0-city,这样 WTForms 就会知道哪个是哪个.

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 %}

(仅在 wtforms 中 l.cityl.form.city 的简写.但是,该语法似乎与 Jinja 冲突,因此有必要使用模板中的显式 l.form.city.)

(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.)

现在准备提交的数据,只需创建 CompanyForm 并遍历位置:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆