预先填充BooleanField(WTForms) [英] Pre-populating a BooleanField as checked (WTForms)
问题描述
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' ,[validators.Required()])
slug = TextField('Slug',[validators.Required()])
active = BooleanField('Active')
然后,我有一个编辑页面,在其中显示我想编辑的问题的表单。
{{form.question.label}}
{{form.question(value = q.question)}}
{{form.active(value = q.active)}}显示这个问题?
如果'active'为True,我想让BooleanField(复选框) '属性。如果是假的,不要。但我甚至无法弄清楚如何使复选框有一个检查状态,当渲染的形式,更不用说条件部分。
唯一的办法,已经能够得到它显示检查是如果我添加默认= True时定义窗体。但这不是我所需要的。
我已经尝试使用默认,初始,值,选择,而没有运气的渲染形式。我搜索了文档和Google。我想我错过了一些东西! :)
更新
以下是我的观点。也许是这样的问题?
$ $ $ $ $ $ $ $ $ $ $ mod.route('/ q /< slug> / edit',methods = [ 'GET','POST'])
def edit(slug):
form = QuestionForm(request.form,csrf_enabled = False)
q = Question.query(Questionslug == 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)
如果你有一个对象,你可以使用它来填充你的表单,如 form = QuestionForm(obj = my_obj)
。如果您只想设置活动属性,请使用 form = QuestionForm(active = True)
。
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)
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)
.
这篇关于预先填充BooleanField(WTForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!