求WTforms中BooleanField的使用方法?


#1

我想实现一下复选框的功能
class Reg_user_Form(FlaskForm):

access_category = BooleanField()

def init(self, *args, **kwargs):
super(Reg_user_Form, self).init(*args, **kwargs)
self.access_category.data = [(1, ‘国内新闻’), (2, ‘体育新闻’)]

显示不出来,这个BooleanField到底怎么使用呀???


#2

BooleanField()是单选框
复选框用SelectField().

有你实现的这个用法吗?我也是新手


#3

class wtforms.fields. BooleanField ( default field arguments , false_values=None )

Represents an <input type="checkbox"> . Set the checked -status by using the default -option. Any value for default , e.g. default="checked" puts checked into the html-element and sets the data to True

Parameters: false_values – If provided, a sequence of strings each of which is an exact match string of what is considered a “false” value. Defaults to the tuple ('false', '')


#4

SelectField是下拉框


#5

不好意思,我记错了
可能是RadioField()?

class wtforms.fields. RadioField ( default field arguments , choices=[] , coerce=unicode )

Like a SelectField, except displays a list of radio buttons.

Iterating the field will produce subfields (each containing a label as well) in order to allow custom rendering of the individual radio fields.

{% for subfield in form.radio %} <tr> <td>{{ subfield }}</td> <td>{{ subfield.label }}</td> </tr> {% endfor %}

Simply outputting the field without iterating its subfields will result in a <ul> list of radio choices.


#6

不太明白你想实现的效果,RadioField实现的是单选按钮,如果是多个复选框的话直接实例化多个BooleanField就可以了吧,否则在视图中处理提交数据也很麻烦


#7

解决如下:
access_category = SelectMultipleField(
label=“可管理栏目”,
choices=([1,‘国内新闻’],[2,‘体育新闻’])
widget=widgets.ListWidget(prefix_label=False),
option_widget=widgets.CheckboxInput(),
coerce=int,
default=[1, 2]
)