代码非常明显 - 我想将一个变量传递给FlaskForm
子类以供进一步使用。
from flask import Flask, render_template_string
from flask_wtf import FlaskForm
from wtforms import StringField
from flask_wtf.csrf import CSRFProtect
app = Flask(__name__)
spam = 'bar'
app.secret_key = 'secret'
csrf = CSRFProtect(app)
@app.route('/')
def index():
eggs = spam
form = FooForm(eggs)
return render_template_string(
'''
{{ form.bar_field.label }} {{ form.bar_field }}
''',form = form)
class FooForm(FlaskForm):
def __init__(self, bar):
super(FooForm, self).__init__()
self.bar = bar
bar_field = StringField("Label's last word is 'bar': {0}".format(self.bar))
if __name__ == '__main__':
app.run(debug=True)
我得到的是
line 22, in FooForm
bar_field = StringField("Label's last word is 'bar': {0}".format(self.bar))
NameError: name 'self' is not defined
我如何实现所需?
答案 0 :(得分:1)
我有一个与此非常相似的问题,花了我一段时间才弄清楚。您想要做的事情可以像这样完成:
setDT(d1)[, as.list(coef(lm(val3 ~ val1 + val2))), id][d1, on = .(id)]
在此处查看我提出的问题和答案:flaskform pass a variable (WTForms)
答案 1 :(得分:0)
将您的bar_field = StringField("Label's last word is 'bar': {0}".format(self.bar))
置于 init ()方法中。
用这个:
class FooForm(FlaskForm):
def __init__(self, bar):
super(FooForm, self).__init__()
self.bar = bar
self.bar_field = StringField("Label's last word is 'bar': {0}".format(self.bar))