- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个脚本,通常从数据库中检索 5 行,我想将其显示为复选框列表。
但它无法正确显示:它显示“UnboundField ”
form.py
class ExampleForm(FlaskForm):
[...query & results...]
for line in results_sql:
list_checkbox[line.label] = BooleanField(line.label)
routes.py
@bp.route('/example')
def example():
form = ExampleForm()
return render_template("index.html", form=form)
index.html
<table class="table table-bordered table-condensed">
{% for checkbox in form.list_checkbox %}
<tr>
<td>{{ checkbox }}</td>
<td>{{ form.list_checkbox[checkbox ] }}</td>
</tr>
{% endfor %}
</table>
结果:
最佳答案
您已将字段放入嵌套字典中。该表单无法绑定(bind)此类字段,因为它无法处理任意容器。
相反,您需要将字段放在 field enclosure 中。我会使用 FormField()
field指向嵌套的 Form
类。您可以通过调用 BaseForm()
constructor 来生成嵌套的 Form
类。 :
What BaseForm provides is a container for a collection of fields, which it will bind at instantiation, and hold in an internal dict. Dict-style access on a BaseForm instance will allow you to access (and modify) the enclosed fields.
然后,当您创建 ExampleForm()
类的实例时,它将绑定(bind) FormField
字段,然后该字段又创建嵌套表单的实例对象,然后绑定(bind)您提供的每个字段 BaseForm()
因为调用 BaseForm(fields)
将创建一个表单实例,因此您需要先将其包装在函数中,然后才能将其用作嵌套表单:
def form_from_fields(fields):
def create_form(prefix='', **kwargs):
form = BaseForm(fields, prefix=prefix, meta=FlaskForm.Meta)
form.process(**kwargs)
return form
return create_form
class ExampleForm(FlaskForm):
# ...
list_checkbox = FormField(
form_from_fields(
[(line.label, BooleanField(line.label)) for line in results_sql]
)
)
BaseForm()
不像 Form
类那样接受任何数据,因此您需要传递 FormField()
的参数返回实例之前,将 create 实例传递给 .process()
方法。
渲染时迭代 list_checkbox
字段时,您可以直接获取字段并从字段对象中获取标签:
<table class="table table-bordered table-condensed">
{% for checkbox in form.list_checkbox %}
<tr>
<td>{{ checkbox.label }}</td>
<td>{{ checkbox }}</td>
</tr>
{% endfor %}
</table>
演示(使用基础 WTForms 库,但 Flask-WTF 流程相同):
>>> from wtforms.form import BaseForm, Form
>>> from wtforms.fields import BooleanField, FormField
>>> fields = ['Calendrier', 'Commentaire', 'Dessin', 'Ex-libris', 'Gravure']
>>> def form_from_fields(fields):
... def create_form(prefix='', **kwargs):
... form = BaseForm(fields, prefix=prefix)
... form.process(**kwargs)
... return form
... return create_form
...
>>> class ExampleForm(Form):
... list_checkbox = FormField(form_from_fields([(field, BooleanField(field)) for field in fields]))
...
>>> form = ExampleForm()
>>> form.list_checkbox
<wtforms.fields.core.FormField object at 0x1232a76d8>
>>> list(form.list_checkbox)
[<wtforms.fields.core.BooleanField object at 0x1232a77f0>, <wtforms.fields.core.BooleanField object at 0x1232a78d0>, <wtforms.fields.core.BooleanField object at 0x1232a7978>, <wtforms.fields.core.BooleanField object at 0x1232a7a20>, <wtforms.fields.core.BooleanField object at 0x1232a7ac8>]
>>> print(*form.list_checkbox, sep='\n')
<input id="list_checkbox-Calendrier" name="list_checkbox-Calendrier" type="checkbox" value="y">
<input id="list_checkbox-Commentaire" name="list_checkbox-Commentaire" type="checkbox" value="y">
<input id="list_checkbox-Dessin" name="list_checkbox-Dessin" type="checkbox" value="y">
<input id="list_checkbox-Ex-libris" name="list_checkbox-Ex-libris" type="checkbox" value="y">
<input id="list_checkbox-Gravure" name="list_checkbox-Gravure" type="checkbox" value="y">
FormField()
字段还确保您可以为表单设置默认值,或者确保您可以在再次发布表单时访问数据集:
>>> form = ExampleForm(list_checkbox={'Calendrier': True})
>>> print(form.list_checkbox['Calendrier'])
<input checked id="list_checkbox-Calendrier" name="list_checkbox-Calendrier" type="checkbox" value="y">
>>> print(form.list_checkbox['Commentaire'])
<input id="list_checkbox-Commentaire" name="list_checkbox-Commentaire" type="checkbox" value="y">
关于python - Flask WTForms BooleanField UnboundField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53340806/
应用程序 from flask import Flask, render_template from flask_wtf import FlaskForm from wtforms import St
我正在编写一个脚本,通常从数据库中检索 5 行,我想将其显示为复选框列表。 但它无法正确显示:它显示“UnboundField ” form.py class ExampleForm(FlaskFor
为什么会出现此错误?什么是 UnboundField?我需要知道什么才能在将来修复和避免此问题? 访问 http://127.0.0.1:5000/signup 时的调试输出: AttributeEr
我是一名优秀的程序员,十分优秀!