WTForms - dynamic labels by passing argument to constructor?

久未见 提交于 2019-12-12 11:18:57

问题


Is it possible to make my form labels dynamic by passing an argument to the form constructor?

I'm thinking of something a bit like the following code:

class MyForm(Form):

    def __init__(self, fruit):
        Form.__init__(self)
        self.fruit = fruit

    name = StringField('Do you like' + fruit + "?")

@app.route('/' ,methods=["GET","POST"])
def home():
    form = NameForm("bananas")

Whatever I try, the text input box's label never seems to be able to access the variable 'fruit' - i.e. I can't seem to make the label dynamic.

It might just be that I don't understand Python objects well enough to see why this is not possible.

来源:https://stackoverflow.com/questions/24469378/wtforms-dynamic-labels-by-passing-argument-to-constructor

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