如何在 Flask-login 中调用 is_authenticated
How to call is_authenticated in Flask-login
我正在与以下用户一起使用 flask-login class。起初我没有覆盖 is_authenticated()
方法,但我认为它可能会解决我的问题(但没有)。
我 运行 我的代码在 3 台不同的计算机上。在其中一个上,我必须将 is_authenticated()
方法作为常规方法调用(使用 ()
),而在另一个 2 上,我必须将 is_authenticated 用作布尔值(没有 ()
).
在所有计算机中,解释器都设置为 python2.7.
我错过了什么?
class User(UserMixin):
"""
Defines each user.
"""
def __init__(self, user, pwd):
self.username = user
self.password = pwd
def get_id(self):
return self.username
def is_authenticated(self):
return True
这是我调用的方法:
@app.before_request
def before_request():
g.user = flask.ext.login.current_user
g.username = g.user.get_id()
@app.route('/', methods=['GET', 'POST'])
@flask.ext.login.login_required
def index():
if g.user is None or not g.user.is_authenticated(): // or g.user.is_authenticated
return redirect('login')
return render_template('main.html', user=g.username)
像这样?检查它是布尔值还是函数:
if type(is_authenticated) == type(True): # if it's a boolean, this is True.
# Your code
else: # if not, it's a function.
is_authenticated()
# Your code
在这里创建一个函数是个好主意:
def function_name():
if type(is_authenticated) == type(True):
return is_authenticated
else:
return is_authenticated()
我正在与以下用户一起使用 flask-login class。起初我没有覆盖 is_authenticated()
方法,但我认为它可能会解决我的问题(但没有)。
我 运行 我的代码在 3 台不同的计算机上。在其中一个上,我必须将 is_authenticated()
方法作为常规方法调用(使用 ()
),而在另一个 2 上,我必须将 is_authenticated 用作布尔值(没有 ()
).
在所有计算机中,解释器都设置为 python2.7.
我错过了什么?
class User(UserMixin):
"""
Defines each user.
"""
def __init__(self, user, pwd):
self.username = user
self.password = pwd
def get_id(self):
return self.username
def is_authenticated(self):
return True
这是我调用的方法:
@app.before_request
def before_request():
g.user = flask.ext.login.current_user
g.username = g.user.get_id()
@app.route('/', methods=['GET', 'POST'])
@flask.ext.login.login_required
def index():
if g.user is None or not g.user.is_authenticated(): // or g.user.is_authenticated
return redirect('login')
return render_template('main.html', user=g.username)
像这样?检查它是布尔值还是函数:
if type(is_authenticated) == type(True): # if it's a boolean, this is True.
# Your code
else: # if not, it's a function.
is_authenticated()
# Your code
在这里创建一个函数是个好主意:
def function_name():
if type(is_authenticated) == type(True):
return is_authenticated
else:
return is_authenticated()