1
我想將表單字段的標籤作爲參數傳遞給表單的__init__
。現在我得到一個NameError: name 'self' is not defined
錯誤。將字段的標籤傳遞給WTForms __init__
class MyForm(FlaskForm):
def __init__(self, label_t, **kw):
super(MyForm, self).__init__(**kw)
self.label_t = label_t
name = StringField(self.label_t, validators=[DataRequired()])
f = MyForm("test1", csrf_enabled=False)
我也試圖把名字變到初始化函數, 但我得到AttributeError: 'UnboundField' object has no attribute '__call__'
。
class MyForm(FlaskForm):
def __init__(self, label_t, **kw):
super(MyForm, self).__init__(**kw)
self.label_t = label_t
self.name = StringField(self.label_t, validators=[DataRequired()])
如何傳遞的標籤字段來__init__
?
謝謝!說得通 :) – squeck