gpt4 book ai didi

python - 尝试在 Flask View 中实例化 WTForms 对象时出现 "Runtime Error: working outside of application context"

转载 作者:行者123 更新时间:2023-11-30 23:10:02 24 4
gpt4 key购买 nike

所以我有一个这样定义的表单:

class NewDashForm(Form):
tabname = StringField('tabname', validators=[DataRequired(),Length(1,16)])
title = StringField('title', validators=[DataRequired(),Length(1,128)])

我将它导入到我的views.py 文件中,但是当我尝试像这样实例化它时:

form = NewDashForm()

我收到“在应用程序上下文之外工作”错误。该应用程序是通过“工厂”方法生成的,如下所示:

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

我在最后一个代码片段中遗漏了一些与数据库相关的内容,但您明白了。根据我的理解, View 中发生的任何事情都应该自动在应用程序上下文中,但显然我遗漏了一些东西。我在这里做错了什么?

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

最佳答案

您应该只在 View 函数中实例化表单,在请求之外这样做是没有意义的。您收到错误是因为它试图从请求中获取数据,但该数据不存在。

如果您确实需要事先实例化它,可以将空数据传递给它,这样它就不会查看请求,或者使用请求上下文。

from werkzeug.datastructrues import MultiDict
form = MyForm(MultiDict())

# or

with app.test_request_context():
form = MyForm()

同样,您永远不应该在应用程序中执行此操作,表单仅在请求期间在 View 内有用。如果您想在单元测试中测试表单,测试请求上下文将会很有用。

关于python - 尝试在 Flask View 中实例化 WTForms 对象时出现 "Runtime Error: working outside of application context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30851513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com