TypeError: 'bool' object is not callable g.user.is_authenticated()
TypeError: 'bool' object is not callable g.user.is_authenticated()
我正在尝试在我的 Flask 应用程序中执行此操作。但是我收到这样的错误
TypeError: 'bool' object is not callable.
对应代码如下:
@app.before_request
def before_request():
g.user = current_user
if g.user.is_authenticated():
g.search_form = None
这是因为 is_authenticated
不是函数,只是一个布尔值。请尝试以下操作:
@app.before_request
def before_request():
g.user = current_user
if g.user.is_authenticated:
g.search_form = None
尝试将 if g.user.is_authenticated():
替换为 if g.user.is_authenticated:
,如下所示:
@app.before_request
def before_request():
g.user = current_user
if g.user.is_authenticated:
g.search_form = None
来自 the document:
is_authenticated
Returns True if the user is authenticated, i.e. they have provided valid credentials. (Only authenticated users will fulfill the criteria of login_required.)
正如文档所说,is_authenticated 是一个布尔值(True
或 False
)。
然而,它在过去是一个函数,but it has been changed to boolean at version 3.0:
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.
我正在尝试在我的 Flask 应用程序中执行此操作。但是我收到这样的错误
TypeError: 'bool' object is not callable.
对应代码如下:
@app.before_request
def before_request():
g.user = current_user
if g.user.is_authenticated():
g.search_form = None
这是因为 is_authenticated
不是函数,只是一个布尔值。请尝试以下操作:
@app.before_request
def before_request():
g.user = current_user
if g.user.is_authenticated:
g.search_form = None
尝试将 if g.user.is_authenticated():
替换为 if g.user.is_authenticated:
,如下所示:
@app.before_request
def before_request():
g.user = current_user
if g.user.is_authenticated:
g.search_form = None
来自 the document:
is_authenticated
Returns True if the user is authenticated, i.e. they have provided valid credentials. (Only authenticated users will fulfill the criteria of login_required.)
正如文档所说,is_authenticated 是一个布尔值(True
或 False
)。
然而,它在过去是一个函数,but it has been changed to boolean at version 3.0:
BREAKING:
Theis_authenticated
,is_active
, andis_anonymous
members of the user class are now properties, not methods. Applications should update their user classes accordingly.