WTForms - dynamic labels by passing argument to co

2019-06-27 04:45发布

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.

0条回答
登录 后发表回答