gpt4 book ai didi

python - 如何在 Flask-login 中调用 is_authenticated

转载 作者:行者123 更新时间:2023-12-01 04:24:47 25 4
gpt4 key购买 nike

我正在将 Flask-login 与以下用户类一起使用。起初我没有覆盖 is_authenticated()方法,但我认为它可能会解决我的问题(但没有)。

我在 3 台不同的计算机上运行了我的代码。对于其中一个,我必须调用 is_authenticated()方法作为常规方法(使用 () ),在另外两个方法上,我必须使用 is_authenticated 作为 bool 值(不使用 () )。

在所有计算机中,解释器都设置为 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)

最佳答案

像这样吗?检查它是 bool 值还是函数:

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()

关于python - 如何在 Flask-login 中调用 is_authenticated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33274337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com