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

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

问题描述

当尝试使用 WTForms 设置 SelectField 的默认值时,我像这样将值传递给默认"参数.

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)

我也尝试了以下方法.

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天全站免登陆