2013-05-22 39 views
1

我得到這個錯誤,我想要任何輸入,因爲它現在是磚牆。AttributeError:'RequestContext'對象沒有'user'屬性

我有一個應用程序,即使用flask-security,通過flask-login導入current_user。任何集成問題都是過去並且已經有一段時間了。

我需要過濾某些特定信息的請求,這些信息放在了g上。我的藍圖工作,任何集成問題都是過去的事情。

昨天,我將這個請求過濾藍圖功能提取到general extension,它基本上將信息過濾到g,並且變得更容易配置,所以它或多或少已經準備好並經過測試。這個擴展替換this blueprint,以創建一個交叉應用程序請求過濾器

我把這個放回到我的應用程序,現在我得到這個,並聲明它是不透明的:我不知道用戶不在或不知道爲什麼RequestContext的

Traceback (most recent call last): 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__ 
    return self.wsgi_app(environ, start_response) 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app 
    response = self.make_response(self.handle_exception(e)) 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app 
    response = self.full_dispatch_request() 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request 
    rv = self.handle_user_exception(e) 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/flask/app.py", line 1356, in full_dispatch_request 
    rv = self.preprocess_request() 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/flask/app.py", line 1539, in preprocess_request 
    rv = func() 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/Flask_Flarf-0.0.1-py2.7.egg/flask_flarf/flarf.py", line 48, in preprocess_request 
    preprocess_func(r) 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/Project_Producer-0.0.1-py2.7.egg/project_producer/config/configs/request_filters_config.py", line 10, in preprocess_with_user 
    g.preprocessed = current_app.extensions['flarf'].preprocess_cls(request) 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/Project_Producer-0.0.1-py2.7.egg/project_producer/config/configs/request_filters_config.py", line 17, in __init__ 
    self.aid = self.determine_account(request) 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/Project_Producer-0.0.1-py2.7.egg/project_producer/config/configs/request_filters_config.py", line 51, in determine_account 
    current_user.account.identifier]) 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/werkzeug/local.py", line 336, in __getattr__ 
    return getattr(self._get_current_object(), name) 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/werkzeug/local.py", line 295, in _get_current_object 
    return self.__local() 
    File "/home/zv/.virtualenvs/j/lib/python2.7/site-packages/flask_login.py", line 403, in <lambda> 
    current_user = LocalProxy(lambda: _request_ctx_stack.top.user) 
AttributeError: 'RequestContext' object has no attribute 'user' 

理論:

1)此功能:

def preprocess_request(preprocess_func=self.preprocess_func): 
     r = _request_ctx_stack.top.request 
     request_endpoint = str(r.url_rule.endpoint).rsplit('.')[-1] 
     if request_endpoint not in _flarf.preprocess_skip: 
      preprocess_func(r) 

那麼,什麼是在跟蹤上述happpening是,preproc ess_request運行調用應用程序本地定義的preprocess_func,它自己創建一個應用程序本地定義的類實例以附加到g,這個本地定義的類實例使用'current_user'...並且這是它的錯誤。

運行,之前或使用_request_ctx_stack尚未與用戶聯繫或刪除用戶。乘爆裂或觸摸_request_ctx_stack.top.request是也許「不好」

2)的操作順序,preprocess_request之前燒瓶的安全運行的添加用戶

3)沒有線索,比特丟失ATM

有什麼建議嗎?

編輯:

問題是最有可能的方法分辨率current_app.before_request_funcs返回:

{None: [<function flarf_filter_request at 0x1a781b8>, <bound method LoginManager._load_user of <flask_login.LoginManager object at 0x1a79990>>, <bound method Principal._on_before_request of <flask_principal.Principal object at 0x1a80290>>]} 

所以主外賣是我做錯了,在那裏,如果我需要得到before_request信息,在before_request時間內得到那些信息,那麼flask運行這些函數並不是最優的,因爲目前沒有before_request函數的排序。

選項?

  1. 添加選項命令before_request功能瓶中,可能作爲優先關鍵字一樣簡單,並與我在做什麼來電分類或有序字典

  2. 改變戰術,例如將其從before_request更改爲裝飾器。但是,這需要一個觀點符號,而創建藍圖和擴展的重點就是不必爲每個視圖收集某些信息。上行將是多個過濾器計劃。

  3. 一個有用的,預先製備:燒瓶優等


回答

0

嘿,我就遇到了這個很好,但與Django的,完全相同的錯誤。所以我認爲這可能是相似的。 RequestContext不是一個請求,但它包含它。它是django中的一個特殊類,當它由你的視圖創建時,它實際上包含請求以及從視圖傳遞給模板的所有其他上下文變量。

這裏是它是如何創建的:

c = RequestContext(request, { 
    'foo': 'bar', 
}) 

更多的是here。並在Flask文檔中。

長話短說,RequestContext是一個在'request'鍵中包含請求的詞典。所以,你可以訪問它是這樣:

request = context['request'] 
user = request.user 

在模板訪問請求,你通常會,request.user應該工作。希望這有幫助,我有一種感覺,這可能在Flask和Django中類似。如果這不起作用,您可以打印出您的上下文並查看它包含的數據。多數民衆贊成我是如何找出我的問題。

乾杯