【问题标题】:Wtforms: How to generate blank value using select fields with dynamic choice valuesWtforms:如何使用具有动态选择值的选择字段生成空白值
【发布时间】:2011-08-17 15:27:50
【问题描述】:

我在 Google App Engine 上使用 Flask 和 WTForms (doc)。为选择字段生成具有空值的字段的最佳方法是什么?

form.group_id.choices = [(g.key().id(), g.name) for g in Group.all().order('name')]

表单字段是否有类似“blank=True”的内容?

myfield = wtf.SelectField()

【问题讨论】:

    标签: python google-app-engine flask wtforms


    【解决方案1】:

    你可以在列表中添加一个空对吗?

    form.group_id.choices.insert(0, ('', ''))
    

    【讨论】:

      【解决方案2】:

      如果是QuerySelectField,可以这样添加参数:

      allow_blank=True, blank_text=u'-- please choose --'
      

      【讨论】:

        【解决方案3】:

        我更喜欢这样的方式:

        form.group_id.choices = ['none'] + [(g.key().id(), g.name) for g in Group.all().order('name')]
        

        这就够了 - 你不需要处理'none' 值。

        【讨论】:

          猜你喜欢
          • 2020-06-03
          • 2012-12-07
          • 2020-07-15
          • 2017-05-05
          • 2015-10-15
          • 2018-12-24
          • 2016-01-05
          • 1970-01-01
          • 2020-09-06
          相关资源
          最近更新 更多