Note that there are some explanatory texts on larger screens.

plurals
  1. POPre-populating a BooleanField as checked (WTForms)
    primarykey
    data
    text
    <p>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...</p> <pre><code>class QuestionForm(Form): question = TextField('Question', [validators.Required()]) slug = TextField('Slug', [validators.Required()]) active = BooleanField('Active') </code></pre> <p>Then I have an EDIT PAGE where I display a form for the 'question' I want to edit.</p> <pre><code>{{ form.question.label }} {{ form.question(value=q.question) }} {{ form.active(value=q.active) }} Show this question? </code></pre> <p>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.</p> <p>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.</p> <p>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! :)</p> <p>UPDATE</p> <p>Here's what my view looks like. Maybe it is the problem?</p> <pre><code>@mod.route('/q/&lt;slug&gt;/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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload