2016-04-29 55 views
0

我想用WTForms(實際上燒瓶WTF)生成此:WTForms BooleanField可以有一個自定義值嗎?

<input id="attr" name="attr" type="checkbox" value="ALL"> Include all attributes 

但如果有任何的方式來指定值BooleanField的一部分,我找不到它。如果我指定:

class MyForm(Form): 
    attr = BooleanField('attr', default=False, description="Include all attributes") 

,並使其在模板:

{{ form.attr }} {{ form.attr.description }} 

然後我得到

<input id="attr" name="attr" type="checkbox" value="y"> Include all attributes 

BooleanField沒有一個 「選項」 或 「值」 屬性組。有什麼辦法可以強制它具有我選擇的價值(例如ALL),而不僅僅是y

+0

看看這裏http://stackoverflow.com/questions/19758112/pre-populating-a-booleanfield-as-checked-wtforms –

+0

謝謝,但這似乎是一個討論如何有條件地使複選框活動(選中)時,而不是如何定製其「值」屬性。 –

回答

1

Booleanfield只能將value屬性設置爲True或False。

SelectFieldRadioField可用於設置具有自定義值的複選框。

class TestForm(Form): 
    Attr_field = SelectField("Attr ", choices=[("ALL", "label")], default="ALL") 

使用SelectMultipleField設置複選框列表並填充它們全部。

相關問題