Pass variable from view to form

女生的网名这么多〃 提交于 2019-12-08 08:21:14

问题


@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)

I know that i can use sessions for this, but probably there is a way of pass the variable from the view to the form class.

Something like this:

form = ProductForm(unique_form, **product_data)

This is possible?


回答1:


Like this:

@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)


来源:https://stackoverflow.com/questions/28684878/pass-variable-from-view-to-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!