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

戏子无情 提交于 2019-12-23 23:17:51

问题


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.


回答1:


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.




回答2:


Af far as I've found, it's not a good idea to change the field to a HiddenField.

What you can do however is to leave the field as it is, but replace its widget so that it's rendered as hidden. The relevant widget is wtforms.widgets.HiddenInput.

Note: this was done on Python3.

Extend your form class with a hide_a() method (what you were calling __MakeTemporarilyHidden__):

from wtforms.widgets import HiddenInput

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

def hide_a(self):
    a.widget = HiddenInput()

You might need to set a.data to a certain value. If the field is a SelectField, you might even need to add a _value callable to the field.



来源:https://stackoverflow.com/questions/35988970/flask-wtforms-is-there-a-way-to-make-a-stringfield-in-the-form-temporarily-h

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