Pre-populating a BooleanField as checked (WTForms)

北城以北 提交于 2019-12-06 21:20:18

问题


For the life of me, I can't figure out how to pre-populate a BooleanField with WTForms. I have a field called "active". It defaults to being not checked, and it's not required. So I set it up like...

class QuestionForm(Form):
    question = TextField('Question', [validators.Required()])
    slug = TextField('Slug', [validators.Required()])
    active = BooleanField('Active')

Then I have an EDIT PAGE where I display a form for the 'question' I want to edit.

{{ form.question.label }}
{{ form.question(value=q.question) }}

{{ form.active(value=q.active) }} Show this question?

If 'active' is True, I'd like the BooleanField (checkbox) to have the 'checked' attribute. And if False, not to. But I can't even figure out how to make the checkbox have a checked state, when rendering the form, let alone the conditional part.

The only way, I've been able to get it to show up checked is if I add default=True when defining the form. But that's not what I need.

I've tried using 'default', 'initial', 'value', 'selected' while rendering the form with no luck. And I've searched the docs and Google. I think I'm missing something! :)

UPDATE

Here's what my view looks like. Maybe it is the problem?

@mod.route('/q/<slug>/edit', methods = ['GET', 'POST'])
def edit(slug):
    form = QuestionForm(request.form, csrf_enabled=False)
    q = Question.query(Question.slug==slug).get()
    if request.method=='POST':
        if form.validate_on_submit():
            q.question = form.data.get('question')
            q.slug = form.data.get('slug')
            q.active = form.data.get('active')
            q.put()
            return redirect('/questions')
    return render_template('questions/edit.html', form=form, q=q)

回答1:


If you have an object you can use it to populate your form like form = QuestionForm(obj=my_obj). If you only want to set the active attribute use form = QuestionForm(active=True).




回答2:


snahor's answer helped after much searching (+1). The google seems weak on this question. I found I needed

<div class="form-group">
  {{adminForm.is_admin.label}}
  {{adminForm.is_admin(checked=True, class_="form-control")}}
</div>

<div class="form-group">
  {{adminForm.is_admin.label}}
  {{adminForm.is_admin(checked=False, class_="form-control")}}
</div>

which I have utilised as

<div class="form-group">
  {{adminForm.is_admin.label}}
  {{adminForm.is_admin(checked=user.is_admin, class_="form-control")}}
</div>



回答3:


To have the default boolean value as True, you need to set the default to "checked"

Basic fields Basic fields generally represent scalar data types with single values, and refer to a single input from the form.

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

Source

class QuestionForm(Form):
    question = TextField('Question', [validators.Required()])
    slug = TextField('Slug', [validators.Required()])
    active = BooleanField('Active', default="checked")



回答4:


In addition to specifying in the template, you can likewise specify in the class definition

class QuestionForm(Form):
    question        = TextField('Question', [validators.Required()])
    slug            = TextField('Slug'    , [validators.Required()])
    activeChecked   = BooleanField('Active', default=True  )
    activeUnChecked = BooleanField('Active', default=False )


来源:https://stackoverflow.com/questions/19758112/pre-populating-a-booleanfield-as-checked-wtforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!