簡體   English   中英

Flask WTForms BooleanField UnboundField

[英]Flask WTForms BooleanField UnboundField

我正在編寫一個通常從數據庫中檢索 5 行的腳本,我想將其顯示為復選框列表。

但它沒有正確顯示:它說“ UnboundField ”

表單.py

class ExampleForm(FlaskForm):
    [...query & results...]
    for line in results_sql:
        list_checkbox[line.label] = BooleanField(line.label)

路線.py

@bp.route('/example')
def example():
    form = ExampleForm()
    return render_template("index.html", form=form)

索引.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>

結果:

渲染表

您已將字段放入嵌套字典中。 表單不能綁定這樣的字段,因為它不能處理任意容器。

相反,您需要將字段放在字段外殼中 我會使用一個FormField()字段來指向一個嵌套的Form類。 您可以通過調用BaseForm()構造函數來生成嵌套的Form類:

BaseForm提供的是一個字段集合的容器,它將在實例化時綁定,並保存在內部字典中。 BaseForm 實例上的字典式訪問將允許您訪問(和修改)封閉的字段。

然后,當您創建ExampleForm()類的實例時,它將綁定FormField字段,然后該字段又會創建嵌套表單對象的實例,然后綁定您提供給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>

Demo(使用基礎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">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM