2015-10-07 43 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类和代码。

相关问题