本文介绍了烧瓶,WTForms:有没有办法使_fieldrily_隐藏形式的StringField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class F(Form):
a = StringField('a',validators是否有一种方法可以在表单中创建一个StringField()方法隐?例如:
@ app.route('/ f',methods = ['GET','POST'])
def f():
form = F(request.form)
if foo(form):
form.a .__ MakeTemporarilyHidden __()
else:
form.a .__ MakeItVisibleAgain __()
if request.method =='GET':
return render_template('f.html',form = form)
我知道wtforms.fields.HiddenField,但是我想在StringField和HiddenField之间动态切换,并返回。
class F(Form):
a = StringField('a',validators = [validators.DataRequired()])
def __init __(self,* args,** kwargs):
hide_a = kwargs。 pop('hide_a')
super(F,self).__ init __(* args,** kwargs)
if hide_a:
self.a.widget = widgets.HiddenInput()
@ app.route('/ f',方法如果foo():
form = F(request.form,hide_a = True)
(),则
def f() else:
form = F(request.form)
if request.method =='GET':
return render_template('f.html',form = form)
尽管在视图中可以像你一样完成,但是最好在Form类中保留表单逻辑。 / p>
This is my model:
class F(Form):
a = StringField('a', validators = [validators.DataRequired()])
Is there a way to make a StringField in the form temporarily hidden? Something like:
@app.route('/f', methods = ['GET', 'POST'])
def f():
form = F(request.form)
if foo(form):
form.a.__MakeTemporarilyHidden__()
else:
form.a.__MakeItVisibleAgain__()
if request.method == 'GET':
return render_template('f.html', form = form)
I am aware of wtforms.fields.HiddenField but I want to dynamically switch between StringField and HiddenField and back.
解决方案
You can do something like
class F(Form):
a = StringField('a', validators = [validators.DataRequired()])
def __init__(self, *args, **kwargs):
hide_a = kwargs.pop('hide_a')
super(F, self).__init__(*args, **kwargs)
if hide_a:
self.a.widget = widgets.HiddenInput()
@app.route('/f', methods = ['GET', 'POST'])
def f():
if foo():
form = F(request.form, hide_a=True)
else:
form = F(request.form)
if request.method == 'GET':
return render_template('f.html', form = form)
Although it can be done in views too like you did, but it is better to keep form logic in your Form class.
这篇关于烧瓶,WTForms:有没有办法使_fieldrily_隐藏形式的StringField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!