[英]Set default in WTForms SelectField from Flask app config?
I'm having difficulty setting a default value in a WTForms SelectField based on a configuration value in my Flask app. 我难以根据我的Flask应用程序中的配置值在WTForms SelectField中设置默认值。
I have a forms.py
file, which includes, in part: 我有一个
forms.py
文件,其中包括:
class MySearchForm(FlaskForm):
title = StringField('Title')
page_size = SelectField('Entries/page', choices=[(10, "10"), (25, "25"), (50, "50"), (100, "100")], default=25)
# other stuff
Even though it would seem to be the right place for this, I can't make the default a variable by passing the app config to the this file, because I get a "Working outside of application context" error. 即使这似乎是正确的地方,我也无法通过将应用程序配置传递给此文件来将默认值设置为变量,因为我收到“在应用程序上下文之外工作”错误。 So I figure I have to do it with a dynamic default at runtime in my Flask view.
因此,我认为必须在Flask视图中使用运行时的动态默认值来执行此操作。
I've looked at a number of other StackOverflow questions about this—the main one seems to be How do you set a default value for a WTForms SelectField? 我已经看过其他有关此问题的StackOverflow问题,主要的问题似乎是如何为WTForms SelectField设置默认值? —but I can't get any of them to work.
-但是我不能让他们任何一个工作。
My routes.py
for the search view is more or less like this: 我的搜索视图的
routes.py
如下所示:
form = MySearchForm(request.args)
if request.args: # if not, I display the search form
if form.title.data:
# construct relevant part of database query
page_size = int(form.page_size.data)
# other stuff: construct and execute DB query; render the view_results page
If, before the if request.args
statement, I try form.page_size.default = current_app.config['PAGE_SIZE']
and then form.process()
, as suggested, this wipes out anything else in the search form, so title
won't go through. 如果在
if request.args
语句之前,我尝试form.page_size.default = current_app.config['PAGE_SIZE']
,然后form.process()
,那么这将消除搜索表单中的其他内容,因此赢得了title
没有经过。 If I try form.page_size.data = current_app.config['PAGE_SIZE']
, it doesn't set the config value as a default in the form, just in the result. 如果我尝试
form.page_size.data = current_app.config['PAGE_SIZE']
,则它不会在表单中将配置值设置为默认值,而只是在结果中。
I have also tried the technique discussed in the Setting default value after initialization in SelectField flask-WTForms question, changing my form
call to form = MySearchForm(request.args, page_size=current_app.config['PAGE_SIZE'])
. 我还尝试了在SelectField flask-WTForms问题中初始化后设置默认值中讨论的技术,将
form
调用更改为form = MySearchForm(request.args, page_size=current_app.config['PAGE_SIZE'])
。 This also doesn't work correctly: On the initial call, the form has nothing selected (and in this specific case, thus shows the value of "10"); 这也不能正常工作:在初始调用中,表单没有被选择(在这种情况下,因此显示值为“ 10”); the configured value is correctly used for the search itself, but the form value of
page_size
remains at "10". 配置的值可正确用于搜索本身,但
page_size
的表单值仍为“ 10”。 Furthermore, actually selecting a value in the form has no effect; 此外,实际上选择表格中的值没有任何作用; the configured value is used regardless of what the user selects in the form.
无论用户在表单中选择什么,都将使用配置的值。 (Similarly, manually changing
page_size
in the URL has the same behavior.) (类似地,在URL中手动更改
page_size
具有相同的行为。)
How am I meant to do this? 我该怎么做呢?
You need to check the consistence of the types of the variables you want to set. 您需要检查要设置的变量类型的一致性。
For that, in your SelectField
, use the parameter coerce
. 为此,请在您的
SelectField
中使用参数coerce
。
If the variable app.config['PAGE_SIZE']
is an int
, you want to declare your form field like so : 如果变量
app.config['PAGE_SIZE']
是一个int
, app.config['PAGE_SIZE']
声明表单字段,如下所示:
page_size = SelectField('Entries/page',
choices=[(10, "10"), (25, "25"), (50, "50"), (100, "100")],
default=25,
coerce=int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.