Python is_authenticated() 引发 TypeError TypeError: 'bool' object is not callable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32983133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is_authenticated() raises TypeError TypeError: 'bool' object is not callable
提问by Gaoyang
I tried to use is_authenticated()
in a view, but got the error `TypeError: 'bool' object is not callable. Why am I getting this error and how do I fix it?
我尝试is_authenticated()
在视图中使用,但收到错误 `TypeError: 'bool' object is not callable。为什么我会收到此错误以及如何修复它?
@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'))
采纳答案by Sepehr Hamzelooy
"object is not callable" error occurs when you are trying to behave an object like it is a method or function.
当您尝试将对象作为方法或函数进行操作时,会发生“对象不可调用”错误。
in this case:
在这种情况下:
current_user.is_authenticated()
you are behaveing current_user.is_authenticated as a method but its not a method .
您将 current_user.is_authenticated 视为一种方法,但它不是方法。
you have to use it in this way :
你必须以这种方式使用它:
current_user.is_authenticated
you use "( )" after methods or functions, not objects.
在方法或函数后使用“( )”,而不是对象。
In some cases a class might implement __call__
function which you can call an object too, then it will be callable.
在某些情况下,一个类可能实现了__call__
您也可以调用一个对象的函数,然后它将是可调用的。
回答by Peter
From Flask-Login 0.3.0(released on September 10th, 2015) changes:
从Flask-Login 0.3.0(2015 年 9 月 10 日发布)更改:
- BREAKING: The
is_authenticated
,is_active
, andis_anonymous
members of the user class are now properties, not methods. Applications should update their user classes accordingly.
- 掰:本
is_authenticated
,is_active
和is_anonymous
用户类的成员现在的属性,而不是方法。应用程序应相应地更新其用户类。
So you need to change your user
class and code accordingly.
所以你需要相应地改变你的user
类和代码。