蓝图中的烧瓶request.args抛出RuntimeError:在请求上下文之外工作

时间:2018-12-05 17:42:41

标签: python flask

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

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

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

如果我想启动我的休息服务烧瓶,则会引发错误:

RuntimeError:在请求上下文之外工作。

我不明白我做错了什么。我在蓝图文档中找不到如何在蓝图中正确获取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.

1 个答案:

答案 0 :(得分:0)

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

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

感谢帮助;)