is_authenticated()引发TypeError TypeError:'bool'对象不可调用 [英] is_authenticated() raises TypeError TypeError: 'bool' object is not callable
问题描述
我试图在视图中使用is_authenticated()
,但收到错误`TypeError:'bool'对象不可调用.为什么会出现此错误以及如何解决?
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?
@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'))
推荐答案
当您尝试表现对象的方法或函数之类的行为时,发生对象不可调用"错误.
"object is not callable" error occurs when you are trying to behave an object like it is a method or function.
在这种情况下:
current_user.is_authenticated()
您将current_user.is_authenticated当作一种方法,而不是一种方法.
you are behaveing current_user.is_authenticated as a method but its not a method .
您必须以这种方式使用它:
you have to use it in this way :
current_user.is_authenticated
您在方法或函数之后使用()",而不是对象.
you use "( )" after methods or functions, not objects.
在某些情况下,一个类可能实现了__call__
函数,您也可以调用该对象,然后该对象将是可调用的.
In some cases a class might implement __call__
function which you can call an object too, then it will be callable.
这篇关于is_authenticated()引发TypeError TypeError:'bool'对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!