在 SelectField flask-WTForms 初始化后设置默认值
Setting default value after initialization in SelectField flask-WTForms
我想在向用户显示时预先 select SelectField
的值。 default
参数在实例化时传递时起作用,但在字段初始化后不起作用。
class AddressForm(Form):
country = SelectField('Country',choices=[('GB', 'Great Britan'), ('US', 'United States')], default='GB') # works
当我尝试在将表单呈现给用户进行编辑之前将 default
值用于前置 select 选项时,它不起作用。
address_form = AddressForm()
address_form.country.default='US' # doesnot work
需要一个解决方案来在呈现给用户之前将默认值设置为预设值。
情况 2:同样不起作用
class AddressForm(Form):
country = SelectField('Country') # works
address_form = AddressForm()
address_form.country.choices=[('GB', 'Great Britan'), ('US', 'United States')]
address_form.country.default='US' # doesnot work
一旦创建了表单实例,数据就被绑定了。之后更改默认值不会执行任何操作。更改 choices
起作用的原因是它会影响验证,直到调用 validate
才会 运行。
将默认数据传递给 form constructor, and it will be used if no form data was passed。默认会第一次渲染,如果用户不更改值,则第二次发布。
form = AddressForm(request.form, country='US')
(如果您使用的是 Flask-WTF 的 Form
,则可以省略 request.form
部分。)
我知道您可能已经解决了问题。但我认为它不再起作用了。因为这是用谷歌搜索问题时出现的第一件事,所以我想为遇到这个问题的人提供一个可行的解决方案(至少对我而言)。
要确认默认选择的更改,我们必须添加 address_form.process()
.
就是这样!
完整的解决方案是:
class AddressForm(Form):
country = SelectField('Country') # works
address_form = AddressForm()
address_form.country.choices=[('GB', 'Great Britan'), ('US', 'United States')]
address_form.country.default='US'
address_form.process() # works
class AddressForm(Form):
country = SelectField('Country') # works
address_form = AddressForm()
address_form.country.choices=[('GB', 'Great Britan'), ('US', 'United States')]
address_form.country.default='US'
address_form.process()
此方法也显示默认值,但当我提交表单时未提交,验证方法正常工作
我想在向用户显示时预先 select SelectField
的值。 default
参数在实例化时传递时起作用,但在字段初始化后不起作用。
class AddressForm(Form):
country = SelectField('Country',choices=[('GB', 'Great Britan'), ('US', 'United States')], default='GB') # works
当我尝试在将表单呈现给用户进行编辑之前将 default
值用于前置 select 选项时,它不起作用。
address_form = AddressForm()
address_form.country.default='US' # doesnot work
需要一个解决方案来在呈现给用户之前将默认值设置为预设值。
情况 2:同样不起作用
class AddressForm(Form):
country = SelectField('Country') # works
address_form = AddressForm()
address_form.country.choices=[('GB', 'Great Britan'), ('US', 'United States')]
address_form.country.default='US' # doesnot work
一旦创建了表单实例,数据就被绑定了。之后更改默认值不会执行任何操作。更改 choices
起作用的原因是它会影响验证,直到调用 validate
才会 运行。
将默认数据传递给 form constructor, and it will be used if no form data was passed。默认会第一次渲染,如果用户不更改值,则第二次发布。
form = AddressForm(request.form, country='US')
(如果您使用的是 Flask-WTF 的 Form
,则可以省略 request.form
部分。)
我知道您可能已经解决了问题。但我认为它不再起作用了。因为这是用谷歌搜索问题时出现的第一件事,所以我想为遇到这个问题的人提供一个可行的解决方案(至少对我而言)。
要确认默认选择的更改,我们必须添加 address_form.process()
.
就是这样!
完整的解决方案是:
class AddressForm(Form):
country = SelectField('Country') # works
address_form = AddressForm()
address_form.country.choices=[('GB', 'Great Britan'), ('US', 'United States')]
address_form.country.default='US'
address_form.process() # works
class AddressForm(Form):
country = SelectField('Country') # works
address_form = AddressForm()
address_form.country.choices=[('GB', 'Great Britan'), ('US', 'United States')]
address_form.country.default='US'
address_form.process()
此方法也显示默认值,但当我提交表单时未提交,验证方法正常工作