【问题标题】:Pass variable from view to form将变量从视图传递到表单
【发布时间】:2015-02-24 06:42:56
【问题描述】:
@app.route('/product/<unique_form>',  methods=['GET', 'POST'])
def product(unique_form):
    form = ProductForm(**product_data)

class ProductForm(Form):
  #form

   def __init__(self, *args, **kwargs):
       #Here in the __init__ I need to access the unique_form value
       Form.__init__(self, *args, **kwargs)

我知道我可以为此使用会话,但可能有一种方法可以将变量从视图传递到表单类。

类似这样的:

form = ProductForm(unique_form, **product_data)

这可能吗?

【问题讨论】:

    标签: python flask flask-wtforms


    【解决方案1】:

    像这样:

    @app.route('/product/<unique_form>',  methods=['GET', 'POST'])
    def product(unique_form):
        form = ProductForm(unique_form, **product_data)
    
    class ProductForm(Form):
       def __init__(self, unique_form, *args, **kwargs):
           # well, now you have unique_form here
           Form.__init__(self, *args, **kwargs)
    

    【讨论】:

      猜你喜欢
      • 2019-05-26
      • 2022-01-15
      • 2018-02-21
      • 1970-01-01
      • 2020-07-04
      • 2019-07-24
      • 2021-09-13
      • 2019-01-15
      • 2023-03-12
      相关资源
      最近更新 更多