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

时间:2017-03-14 10:54:06

标签: python

尝试从URL获取GET参数。我在__init__.py文件中使用它,但是在另一个文件中它无效。

我尝试使用with app.app_context():,但我仍然遇到同样的问题。

def log_entry(entity, type, entity_id, data, error):
    with app.app_context():
        zip_id = request.args.get('id')

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

有什么建议吗?

其他信息:

  • 这是使用设置为服务(API)的Flask Web框架。
  • 用户点击http://website.com/api/endpoint?id=1
  • 的网址示例
  • 如上所述,使用`zip_id = request.args.get(' id')在主文件中正常工作但我在runners.py(只是另一个带有定义的文件)
  • 完全追溯:

Debugging middleware caught exception in streamed response at a point where response headers were already sent. Traceback (most recent call last): File "/Users/ereeve/.virtualenvs/pi-automation-api/lib/python2.7/site-packages/werkzeug/wsgi.py", line 703, in __next__ return self._next() File "/Users/ereeve/.virtualenvs/pi-automation-api/lib/python2.7/site-packages/werkzeug/wrappers.py", line 81, in _iter_encoded for item in iterable: File "/Users/ereeve/Documents/TechSol/pi-automation-api/automation_api/runners.py", line 341, in create_agencies log_entry("test", "created", 1, "{'data':'hey'}", "") File "/Users/ereeve/Documents/TechSol/pi-automation-api/automation_api/runners.py", line 315, in log_entry zip_id = request.args.get('id') File "/Users/ereeve/.virtualenvs/pi-automation-api/lib/python2.7/site-packages/werkzeug/local.py", line 343, in __getattr__ return getattr(self._get_current_object(), name) File "/Users/ereeve/.virtualenvs/pi-automation-api/lib/python2.7/site-packages/werkzeug/local.py", line 302, in _get_current_object return self.__local() File "/Users/ereeve/.virtualenvs/pi-automation-api/lib/python2.7/site-packages/flask/globals.py", line 20, in _lookup_req_object raise RuntimeError('working outside of request context') RuntimeError: working outside of request context

在调用log_entry def的同一文件中定义

def create_agencies(country_code, DB, session):
    document = DB.find_one({'rb_account_id': RB_COUNTRIES_new[country_code]['rb_account_id']})
    t2 = new_t2(session)

    log_entry("test", "created", 1, "{'data':'hey'}", "")

0 个答案:

没有答案