"Runtime Error: working outside of application context" when trying to instantiate WTForms object in Flask view
"Runtime Error: working outside of application context" when trying to instantiate WTForms object in Flask view
所以我有一个这样定义的表单:
class NewDashForm(Form):
tabname = StringField('tabname', validators=[DataRequired(),Length(1,16)])
title = StringField('title', validators=[DataRequired(),Length(1,128)])
我正在将它导入到我的 views.py 文件中,但是当我尝试像这样实例化它时:
form = NewDashForm()
我收到 "working outside of application context" 错误。该应用程序由 "factory" 方法生成,如下所示:
def generate_application(config=None):
application = Flask(__name__)
application.config.from_object(config)
application.register_blueprint(cheda.generate_blueprint(application.config,application.db))
return application
我在最后一个代码片段中遗漏了一些与数据库相关的内容,但您明白了。根据我的理解,视图中发生的任何事情都应该自动处于应用程序上下文中,但显然我遗漏了一些东西。我在这里做错了什么?
Traceback (most recent call last):
File "/vagrant/venv/cheda_env/bin/cheda-run", line 9, in <module>
load_entry_point('cheda==0.1.0', 'console_scripts', 'cheda-run')()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/pkg_resources/__init__.py", line 546, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2666, in load_entry_point
return ep.load()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2339, in load
return self.resolve()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2345, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/code/cheda/cheda/cmd/run.py", line 10, in <module>
from .. import application
File "/code/cheda/cheda/application.py", line 16, in <module>
from .blueprints import cheda
File "/code/cheda/cheda/blueprints/cheda.py", line 10, in <module>
from ..views import views as cheda_views
File "/code/cheda/cheda/views/views.py", line 37, in <module>
class NewDash(JsonApiView):
File "/code/cheda/cheda/views/views.py", line 39, in NewDash
form = NewDashForm()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/wtforms/form.py", line 212, in __call__
return type.__call__(cls, *args, **kwargs)
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/flask_wtf/form.py", line 68, in __init__
csrf_enabled = current_app.config.get('WTF_CSRF_ENABLED', True)
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/werkzeug/local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/werkzeug/local.py", line 297, in _get_current_object
return self.__local()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/flask/globals.py", line 34, in _find_app
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
您应该只在视图函数中实例化表单,在请求之外这样做是没有意义的。您收到错误是因为它试图从不存在的请求中获取数据。
如果您真的需要事先实例化它,要么将空数据传递给它,这样它就不会查看请求,要么使用请求上下文。
from werkzeug.datastructrues import MultiDict
form = MyForm(MultiDict())
# or
with app.test_request_context():
form = MyForm()
同样,您永远不应该在您的应用程序中这样做,表单仅在请求期间在视图内有用。如果您想在单元测试中测试表单,测试请求上下文会很有用。
所以我有一个这样定义的表单:
class NewDashForm(Form):
tabname = StringField('tabname', validators=[DataRequired(),Length(1,16)])
title = StringField('title', validators=[DataRequired(),Length(1,128)])
我正在将它导入到我的 views.py 文件中,但是当我尝试像这样实例化它时:
form = NewDashForm()
我收到 "working outside of application context" 错误。该应用程序由 "factory" 方法生成,如下所示:
def generate_application(config=None):
application = Flask(__name__)
application.config.from_object(config)
application.register_blueprint(cheda.generate_blueprint(application.config,application.db))
return application
我在最后一个代码片段中遗漏了一些与数据库相关的内容,但您明白了。根据我的理解,视图中发生的任何事情都应该自动处于应用程序上下文中,但显然我遗漏了一些东西。我在这里做错了什么?
Traceback (most recent call last):
File "/vagrant/venv/cheda_env/bin/cheda-run", line 9, in <module>
load_entry_point('cheda==0.1.0', 'console_scripts', 'cheda-run')()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/pkg_resources/__init__.py", line 546, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2666, in load_entry_point
return ep.load()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2339, in load
return self.resolve()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2345, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/code/cheda/cheda/cmd/run.py", line 10, in <module>
from .. import application
File "/code/cheda/cheda/application.py", line 16, in <module>
from .blueprints import cheda
File "/code/cheda/cheda/blueprints/cheda.py", line 10, in <module>
from ..views import views as cheda_views
File "/code/cheda/cheda/views/views.py", line 37, in <module>
class NewDash(JsonApiView):
File "/code/cheda/cheda/views/views.py", line 39, in NewDash
form = NewDashForm()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/wtforms/form.py", line 212, in __call__
return type.__call__(cls, *args, **kwargs)
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/flask_wtf/form.py", line 68, in __init__
csrf_enabled = current_app.config.get('WTF_CSRF_ENABLED', True)
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/werkzeug/local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/werkzeug/local.py", line 297, in _get_current_object
return self.__local()
File "/vagrant/venv/cheda_env/lib/python3.4/site-packages/flask/globals.py", line 34, in _find_app
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
您应该只在视图函数中实例化表单,在请求之外这样做是没有意义的。您收到错误是因为它试图从不存在的请求中获取数据。
如果您真的需要事先实例化它,要么将空数据传递给它,这样它就不会查看请求,要么使用请求上下文。
from werkzeug.datastructrues import MultiDict
form = MyForm(MultiDict())
# or
with app.test_request_context():
form = MyForm()
同样,您永远不应该在您的应用程序中这样做,表单仅在请求期间在视图内有用。如果您想在单元测试中测试表单,测试请求上下文会很有用。