【问题标题】:flask wtforms populating drop down list烧瓶 wtforms 填充下拉列表
【发布时间】:2015-03-20 15:05:03
【问题描述】:

这不是一个非常具体的问题,所以提前抱歉。

我有一个名为 Crews 的文件夹,其中包含 json 文件。

我正在使用 WTForms 创建一个下拉列表,我希望在该列表中填充该文件夹中 json 文件的名称,但我不知道该怎么做。

(我正在创建一个可视化所选 json 数据的 Web 应用程序,因此表单将包含一个返回所选 json 数据的函数,该函数由 d3.js 代码调用)

【问题讨论】:

    标签: python json flask wtforms


    【解决方案1】:

    使用它来获取目录中的所有文件:

    How do I list all files of a directory?

    获得 json 文件列表后,您可以使用它来填充 WTForms SelectField

    中的选择
    from flask.ext.wtf import Form
    from wtforms import SelectField
    
    filenames = ['1.json', '2.json'] # This will be generated by you
    class MyForm
        json_file = SelectField(u"Filename", [Optional()], choices=[(f, f) for f in filenames])
    

    【讨论】:

    • 我支持你,但是'[Optional()]' 参数是什么?
    • 应该有对 WTForms 的 post url 引用!
    • 派对很晚,但 Optional() 是一个验证器,它使标签的选择保持可选
    猜你喜欢
    • 2016-01-23
    • 2020-12-19
    • 1970-01-01
    • 2020-04-16
    • 2014-03-15
    • 2013-03-16
    • 1970-01-01
    相关资源
    最近更新 更多