你如何设置一个WTForms SelectField的默认值? [英] How do you set a default value for a WTForms SelectField?

查看:574
本文介绍了你如何设置一个WTForms SelectField的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图设置带有WTForms的SelectField的默认值时,我将值传递给'default'参数,如下所示。

When attempting to set the default value of a SelectField with WTForms, I pass in value to the 'default' parameter like so.

class TestForm(Form):
  test_field = SelectField("Test: ", choices=[(1, "Abc"), (2, "Def")], default=2)

我也尝试了下面的内容。

I have also tried the following.

class TestForm(Form):
  test_field = SelectField("Test: ", choices=[(1, "Abc"), (2, "Def")], default=(2, "Def"))

既不将默认选定字段设置为Def这适用于其他类型的字段,如TextField。如何设置一个SelectField的默认值?

Neither set the default selected field to "Def." This works for other kinds of Fields such as TextField. How do you set the default value for a SelectField?'

推荐答案

您发布的第一个方法是正确的,它适用于我。唯一的解释是它不能正常工作,可能是因为你正在运行一个老版本的WTForms,它在1.0.1上为我工作

The first way you posted is correct, and it works for me. The only explanation for it not working can be that you are running an older version of WTForms, it worked for me on 1.0.1

这篇关于你如何设置一个WTForms SelectField的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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