python - 蓝图中的 Flask request.args 抛出 RuntimeError : Working outside of request context

标签 python flask

我的应用程序是这样构建的:

ma​​in.py

from communication.rest.routes.v1.files import files
from communication.rest.routes.v1.data import data    

APP = Flask(__name__, template_folder='../templates')
APP.register_blueprint(data, url_prefix='/v1/data')
APP.register_blueprint(files, url_prefix='/v1/files') 
...

data.py

from flask import Blueprint, request
data = Blueprint('data', __name__)

@data.route('/days/details', methods=['GET'])
def get_days_details():
   kwargs = request.args.to_dict()
   ...

如果我想启动我的休息服务 flask 会抛出错误:

运行时错误:在请求上下文之外工作。

我不明白我做错了什么。我在蓝图文档中找不到如何在蓝图中正确获取 request.args 的提示

line 64, in get_days_details
    kwargs = request.args.to_dict()
  File "C:\ProgramData\Anaconda3\lib\site-packages\werkzeug\local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\ProgramData\Anaconda3\lib\site-packages\werkzeug\local.py", line 306, in _get_current_object
    return self.__local()
  File "C:\ProgramData\Anaconda3\lib\site-packages\flask\globals.py", line 37, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

最佳答案

我犯了一个错误。因此,如果您在加载模块时看到此异常,则可能是在某个地方调用了该函数(不是由 Flask 引起的)。

如果您不使用请求上下文,这确实有效。但是,如果您使用它,则在没有请求上下文的情况下无法调用它(如果请求到达您的端点,通常由 Flask 生成)。为了进行测试,文档中有一种解决此问题的方法。如果您不进行测试,只需检查此函数的使用情况以及何时调用它。

感谢您的帮助;)

关于python - 蓝图中的 Flask request.args 抛出 RuntimeError : Working outside of request context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53637929/

相关文章:

python - AADSTS50011 : The reply URL specified in the request does not match the reply URLs configured for the application. 如何指定重定向 URI?

python - 我如何有效地将 2d python 列表提供给 C++ 扩展模块

python - 在 Jinja 2 中使用变量作为字典键

python - 如何在 Python 中检查 shell 命令是否结束

python - flask 上传 : How to get file name?

python - WTForms RadioField 如何在没有 <ul> 和 <li> 标签的情况下生成 html?

python - 从 cygwin 运行 numpy

python - 有没有一种优雅的方法可以使用自动生成的参数调用机器人框架测试?

python - 从另一台计算机连接到在 Amazon EC2 上的本地主机上运行的 Web 应用程序

python - Flask-SQLAlchemy : Manipulate attribute of a data upon fetching