将变量从视图传递给表单 [英] Pass variable from view to form

查看:201
本文介绍了将变量从视图传递给表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @ app.route('/ product /< unique_form>',methods = ['GET','POST'])
def product(unique_form):
form = ProductForm(** product_data)






  class ProductForm(Form):
#form
$ b $ def __init __(self,* args,** kwargs):
#Here in __init__我需要访问unique_form值
Form .__ init __(self,* args,** kwargs)

我知道我可以使用会话,但可能有一种方法将视图中的变量传递给窗体类。

类似于这:

  form = ProductForm(unique_form,** product_data)

这是可能的吗?

解决方案

>

  @ app.route('/ product /< unique_form>',methods = ['GET','POST'])
def product(unique_form):
form = ProductForm(unique_fo rm,** product_data)
$ b $ class ProductForm(Form):
def __init __(self,unique_form,* args,** kwargs):
#现在你有unique_form这里
表单.__ init __(self,* args,** kwargs)


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

解决方案

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)

这篇关于将变量从视图传递给表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆