2012-06-20 75 views
2

在回調函數中,我無法訪問flask.session,flask.g或諸如url_for()之類的函數。他們都拋出一個錯誤,說我「在請求範圍之外工作」。Flask-Sijax回調函數「在請求上下文之外工作」

Debugging middleware caught exception in streamed response at a point where response headers were already sent. 
Traceback (most recent call last): 
    File "C:\Python27\site-packages\sijax\response\streaming.py", line 136, in _process_call_chain 
    for string in generator: 
    File "C:\Python27\site-packages\sijax\response\streaming.py", line 109, in _process_callback 
    response = self._perform_handler_call(callback, args) 
    File "C:\Python27\site-packages\sijax\response\base.py", line 258, in _perform_handler_call 
    return callback(self, *args) 
    File "C:\Dropbox\Code\Python 2.7\FlaskTesting\testpage.py", line 18, in myformhandler 
    sql_session = flask.g.sql_session 
    File "C:\Python27\lib\site-packages\werkzeug\local.py", line 336, in __getattr__ 
    return getattr(self._get_current_object(), name) 
    File "C:\Python27\lib\site-packages\werkzeug\local.py", line 295, in _get_current_object 
    return self.__local() 
    File "C:\Python27\lib\site-packages\flask\globals.py", line 19, in _lookup_object 
    raise RuntimeError('working outside of request context') 
RuntimeError: working outside of request context 
192.168.1.141 - - [20/Jun/2012 16:33:04] "POST /testpage HTTP/1.1" 200 - 

我一直無法找出如何解決這個問題。任何幫助,將不勝感激。

  • 的Python V2.7
  • 瓶V0.8
  • 燒瓶Sijax V0.3
+1

粘貼您的代碼,即使您已經解決了您的問題。 – I159

回答

0

這將是有益的,如果你發佈你的代碼,但嘗試包裝你這樣的代碼:

with app.app_context(): 
    # do stuff... 

或也許這:

with app.test_request_context(): 
    # do stuff... 
相關問題