我有一个简单的Python Flask脚本,该脚本应动态填充多个FieldList
中的SelectField
并设置选择。
from flask import Flask
from flask_wtf import FlaskForm
from wtforms import SelectField, FieldList
class Config(object):
SECRET_KEY = 'bla'
app = Flask(__name__)
app.config.from_object(Config)
class TestForm(FlaskForm):
sel = FieldList(SelectField())
@app.route('/')
def set():
# dynamic data to display in the form
data = [
{
'label': 'label1',
'choices': [('choice', 'choice')]
},
{
'label': 'label2',
'choices': [('choice','choice')]
}
]
form = TestForm()
# populate the form somehow here...
print(form.sel)
return 'ok'
if __name__ == '__main__':
app.run()
如何预填充SelectFields?我知道可以将obj
参数传递给TestForm
,并且FieldList也有append_entry
函数,该函数也将对象作为参数({{3} }。但是该对象的结构应该是什么?