蓝图中的Flask request.args引发RuntimeError:在请求上下文之外工作



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

main.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') 
...

数据.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()
...

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

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

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

line 64, in get_days_details
kwargs = request.args.to_dict()
File "C:ProgramDataAnaconda3libsite-packageswerkzeuglocal.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "C:ProgramDataAnaconda3libsite-packageswerkzeuglocal.py", line 306, in _get_current_object
return self.__local()
File "C:ProgramDataAnaconda3libsite-packagesflaskglobals.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生成(。对于测试,文档中有一个如何解决此问题的方法。如果您不进行测试,只需检查此函数的使用情况以及何时调用它。

感谢您的帮助;(

最新更新