SelectFields的预填充FieldList(Flask-WTForms)

时间:2019-11-21 11:58:24

标签: python flask flask-wtforms populate

我有一个简单的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} }。但是该对象的结构应该是什么?

0 个答案:

没有答案