2015-10-07 26 views
6

我試圖在視圖中使用is_authenticated(),但得到錯誤'TypeError:'bool'對象不可調用。爲什麼我得到這個錯誤,我該如何解決?is_authenticated()引發TypeError TypeError:'bool'對象不可調用

@auth.before_app_request 
def before_request(): 
    if current_user.is_authenticated() \ 
      and not current_user.confirmed \ 
      and request.endpoint[:5] != 'auth.' \ 
      and request.endpoint != 'static': 
     return redirect(url_for('auth.unconfirmed')) 
+0

刪除括號 – hsfzxjy

+0

刪除'()'。 –

+0

感謝您的幫助! – Gaoyang

回答

6

當您試圖表現一個對象時,會出現「對象不可調用」錯誤,就像它是方法或函數一樣。

在這種情況下

current_user.is_authenticated() 

你behaveing current_user.is_authenticated作爲一種方法,但它不是一個方法。

你以這種方式來使用它:

current_user.is_authenticated 

您使用「()」後的方法或功能,而不是對象。

在某些情況下,類可能會實現__call__函數,您可以調用一個對象,然後它將被調用。

8

Flask-Login 0.3.0(9月10日公佈,2015年)的變化:

  • BREAKING: The is_authenticated , is_active , and is_anonymous members of the user class are now properties, not methods. Applications should update their user classes accordingly.

所以你需要相應地改變你的user類和代碼。

相關問題