在FieldList中动态调整的表单 [英] Dynamically adjusted forms inside FieldList
问题描述
我正在使用Flask和Flask-WTF,我需要创建一个包含几个具有相似结构的块(子表单)的表单(例如具有一个SelectField
和一个TextAreaField
的子表单).正如文档建议一样,我可以使用FieldList
一起实现此目标.但是,我需要动态调整SelectField
(根据数据库中的值在运行时更改其choice
).现在,文档建议
I'm using Flask and Flask-WTF and I need to create a form that contains several blocks (subforms) of a similar structure (like a subform with one SelectField
and one TextAreaField
). As the docs suggest, I can use FormField
together with FieldList
to achieve this goal. However, I need to tweak my SelectField
's dynamically (changing their choice
s at runtime according to values in the database). The docs now suggest
请注意,choices关键字仅被评估一次,因此,如果要创建动态下拉列表,则需要在实例化后将选择列表分配给字段.
Note that the choices keyword is only evaluated once, so if you want to make a dynamic drop-down list, you’ll want to assign the choices list to the field after instantiation.
可以在此处找到这种方法的示例.但是,FormField
接受的是form_class
,而不是实例(至少根据文档显示),因此看来这两个配方不能很好地配合使用.
One can find an example of this approach here. However, FormField
is accepting form_class
, not an instance (at least, according to the docs), so it seems that these two recipes do not play well together.
还有其他想法吗?
推荐答案
好吧,这比我想象的要容易.您必须使用空白选项创建这些子表单,创建它们的列表,然后在列表项中调整选项.
Okay, it's easier than I thought. You have to create those subforms with empty choices, create list of them and then adjust choices inside the list items.
class UserForm(wtforms.Form):
# must inherit from wtforms.Form, not flask-WTForms'
# see http://stackoverflow.com/questions/15649027/wtforms-csrf-flask-fieldlist
first_name = StringField('First Name', [validators.DataRequired()])
last_name = StringField('Last Name', [validators.DataRequired()])
accepted_policy = BooleanField('Accept Policy')
experience = SelectField('Experience', coerce=int)
class UsersForm(Form):
users = FieldList(FormField(UserForm), min_entries=2)
@app.route('/', methods=['GET', 'POST'])
def hello_world():
form = UsersForm(users=[{}, {}, {}])
form.users[0].experience.choices=[(1, 'One'), (2, 'Two')]
form.users[1].experience.choices = [(1, 'Uno'), (2, 'Du')]
form.users[2].experience.choices = [(0, 'Zero')]
return render_template("hello.html", form=form)
这是一个模板:
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}
<form method=post>
<dl>
{{ form.hidden_tag()}}
{% for user_entry_form in form.users %}
{{ user_entry_form.name }}
{{ render_field(user_entry_form.first_name) }}
{{ render_field(user_entry_form.last_name) }}
{{ render_field(user_entry_form.experience) }}
{% endfor %}
</dl>
<p><input type=submit value=Go!>
</form>
这篇关于在FieldList中动态调整的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!