Python 使用 wtforms 和 flask 在选择字段中设置默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17629291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:44:18  来源:igfitidea点击:

Setting a default value in the selectfield, using wtforms and flask

pythonflaskwtforms

提问by kfk

I am creating a product edit form and I need the form to be pre-populated with the previous data.

我正在创建一个产品编辑表单,我需要用以前的数据预先填充该表单。

I am doing the following:

我正在做以下事情:

Product(form):
    product = TextField('name')
    category = SelectField('category', choice=[(1,'one'),(2,'two')])

In the view:

在视图中:

form.product.data = 'A product name from the database'
form.category.data = 'a category name' #This does not work

The issue is with the SelectField.

问题出在 SelectField 上。

I understand there is a 'default' value I can set on SelectField. However, this happens in the form definition class and there I do not have the query object from sqlalchemy yet.

我知道我可以在 SelectField 上设置一个“默认”值。但是,这发生在表单定义类中,我还没有来自 sqlalchemy 的查询对象。

So, is there a way to append a default on selectfield on run time?

那么,有没有办法在运行时在选择字段上附加默认值?

采纳答案by tbicr

If you want set default value to render page with form you can create own form or set value:

如果您想设置默认值以使用表单呈现页面,您可以创建自己的表单或设置值:

class Product(Form):
    product = TextField('name')
    category = SelectField('category', choices=[(1,'one'),(2,'two')])

# create instance with predefined value:
form1 = Product(category=2)
# form1.product == <input id="product" name="product" type="text" value="">
# form1.category == <select id="category" name="category">
#                     <option value="1">one</option>
#                     <option selected value="2">two</option>
#                   </select>
# from1.product.data == None
# form1.category.data == 2

# create own form if it need many times:
Product2 = type('Product2', (Product,), {
    'category': SelectField('category', default=2, choices=[(1,'one'),(2,'two')])
})
form2 = Product2()
# form2.product == <input id="product" name="product" type="text" value="">
# form2.category == <select id="category" name="category">
#                     <option value="1">one</option>
#                     <option selected value="2">two</option>
#                   </select>
# from2.product.data == None
# form2.category.data == 2

If you want set default form data on request:

如果您想根据要求设置默认表单数据:

with app.test_request_context(method='POST'):
    form = Product(request.form)
    # form5.category.data == None

    form = Product(request.form, category=2)
    # form5.category.data == 2

with app.test_request_context(method='POST', data={'category': 1}):
    form = Product(request.form)
    # form5.category.data == 1

    form = Product(request.form, category=2)
    # form5.category.data == 1

回答by AlexLordThorsen

From the WTForms documentation

来自 WTForms 文档

Note that the choices keyword is only evaluated once, so if you want to make a dynamic drop-down list, you'll want to assign the choices list to the field after instantiation.

请注意,choices 关键字只计算一次,因此如果您想制作动态下拉列表,您需要在实例化后将选择列表分配给字段。

Product(form):
    product = TextField('name')
    category = SelectField('category')

And then in your view say

然后在你看来说

form.category.choices = [list of choices with chosen data]

More detail found here: http://wtforms.simplecodes.com/docs/0.6.1/fields.html#wtforms.fields.SelectField

在这里找到更多细节:http: //wtforms.simplecodes.com/docs/0.6.1/fields.html#wtforms.fields.SelectField