问题
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