【问题标题】:Flask and WTForms - how to get wtforms to refresh select dataFlask 和 WTForms - 如何让 wtforms 刷新选择数据
【发布时间】:2012-08-23 15:41:22
【问题描述】:

我正在使用最新版本的 flask、wtforms 和 Flask-WTForms。

我有一个显示表单的页面,其中一个是带有名为“A”的选项的选择框。

应用启动时一切正常。在另一种形式中,我添加了一条名为“B”的记录。

现在,我想要的表单应该有带有选项 A 和 B 机器人的选择框,只有选项 A 可用。我必须杀死 uWSGI 并重新启动以获取 wtforms 以刷新数据。

那么,我错过了什么?如何让 wtforms 刷新数据?

以下是我创建表单的方法,其中 getAgencyList 返回要添加到选择框的选项列表。在另一个对话中,我添加了一个代理,并且代理列表应该更新而无需重新启动应用程序:

class createUser(Form):
    """
    Users are given a default password
    """
    first_name   = TextField()
    last_name    = TextField()
    email = TextField('Email', [validators.Length(min=6, max=120), validators.Email()])
    user_role = SelectField(u'User Role', choices=[('1', 'User'), ('2', 'Admin')])
    org_role = SelectField(u'User Role', choices=[('1', 'Agency'), ('2', 'Advertiser'),('3', 'Admin')])
    agency = SelectField(u'Agency', choices=getAgencyList())

【问题讨论】:

    标签: python flask wtforms


    【解决方案1】:

    问题是getAgencyList() 在类的定义中被调用。所以那个函数在那个时候返回的都是它的数据。为了更新列表信息,您必须在实例化期间以某种方式运行getAgencyList。为了做到这一点,您可以使用关于 wtforms 的一个不太明显的事实,它允许您向特定字段添加选择。 documentation is here 只需查找标题为“选择具有动态选择值的字段”的小节。这是一个应该可以工作的代码示例。

    class CreateUserForm(Form):
        first_name = TextField()
        last_name = TextField()
        email = TextField('Email', 
                [validators.Length(min=6, max=120), validators.Email()])
        user_role = SelectField(u'User Role', 
                choices=[('1', 'User'), ('2', 'Admin')])
        org_role = SelectField(u'User Role', 
                choices=[('1', 'Agency'), ('2', 'Advertiser'),('3', 'Admin')])
        agency = SelectField(u'Agency')
    
        @classmethod
        def new(cls):
            # Instantiate the form
            form = cls()
    
            # Update the choices for the agency field
            form.agency.choices = getAgencyList()
            return form
    
    # So in order to use you do this ...
    @app.route('/someendpoint')
    def some_flask_endpoint():
        # ... some code ...
        form = CreateUserForm.new()
        # That should give you a working CreateUserForm with updated values.
        # ... some more code to validate form probably...
    

    【讨论】:

      【解决方案2】:

      一个简单的解决方案是从数据库中获取要显示的选项,然后用这些选项覆盖表单类:

      例如:

      def get_agencies():
          agency_list = []
          # get the Agencies from the database - syntax here would be SQLAlchemy
          agencies = Agency.query.all()
          for a in agencies:
              # generate a new list of tuples
              agency_list.append((a.id,a.name))
          return agency_list
      
      @app.route('/somewhere',methods=['POST'])
      def somewhere():
          form = createUser()
          # overwrite the choices of the Form Class
          form.agency.choices = get_agencies()
          # here goes the rest of code - like form.validate_on_submit()
          ...
          return render_template('create_user.html', form=form)
      

      【讨论】:

        猜你喜欢
        • 2012-10-12
        • 2018-04-05
        • 2019-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-03
        相关资源
        最近更新 更多