gpt4 book ai didi

python - flask ,WTForms : Is there a way to make a StringField in the form _temporarily_ hidden?

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:42 24 4
gpt4 key购买 nike

这是我的模型:

class F(Form):
a = StringField('a', validators = [validators.DataRequired()])

有没有办法让表单中的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', 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)

虽然它也可以像您一样在 View 中完成,但是最好将表单逻辑保留在您的 Form 类中。

关于python - flask ,WTForms : Is there a way to make a StringField in the form _temporarily_ hidden?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35988970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com