【问题标题】:Populate the WTF Form Field value using Form inbuilt Function使用表单内置函数填充 WTF 表单字段值
【发布时间】:2020-07-20 06:07:00
【问题描述】:

需要使用函数填充 SelectField(WTF 表单)的选项。我不想在SelectField 本身中添加选择数据。

class TestForm(FlaskForm):
      dropdown = SelectField(choices=[])
      
      def form_overrided_method(self):
           self.dropdown.choices = [('A', 'A')]
    

【问题讨论】:

    标签: python flask flask-wtforms wtforms


    【解决方案1】:

    您可以将值应用于TestForm 类的__init__ 函数中的选项:

    class TestForm(FlaskForm):
        dropdown = SelectField('Dropdown', coerce=int)
    
        def __init__(self, *args, **kwargs):
            super(TestForm, self).__init__(*args, **kwargs)
            self.dropdown.choices = [(1, 'A'),...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 2011-02-18
      相关资源
      最近更新 更多