Python 如何在flask-login中跟踪当前用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19274226/
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
How to track the current user in flask-login?
提问by naga4ce
I m trying to use the current user in my view from flask-login. So i tried to g object
我正在尝试从 Flask-login 的视图中使用当前用户。所以我试图g object
I m assigning flask.ext.login.current_user
to g object
我正在分配flask.ext.login.current_user
给 g 对象
@pot.before_request
def load_users():
g.user = current_user.username
It works if the user is correct. But when i do sign-upor login as with wrong credentials I get this error
如果用户是正确的,它就会起作用。但是当我使用错误的凭据进行注册或登录时,我收到此错误
AttributeError: 'AnonymousUserMixin' object has no attribute 'username'
AttributeError: 'AnonymousUserMixin' object has no attribute 'username'
Please enlight me where am i wrong...
请赐教我错在哪里...
采纳答案by naga4ce
Thanks for your answer @Joe and @pjnola, as you all suggested i referred flask-login docs
感谢您的回答@Joe 和@pjnola,正如你们所建议的,我提到了flask-login docs
I found that we can customize the anonymous user class, so i customized for my requirement,
我发现我们可以自定义匿名用户类,所以我根据我的要求进行了自定义,
Anonymous class
Anonymous class
#!/usr/bin/python
#flask-login anonymous user class
from flask.ext.login import AnonymousUserMixin
class Anonymous(AnonymousUserMixin):
def __init__(self):
self.username = 'Guest'
Then added this class to anonymous_user
然后将这个类添加到 anonymous_user
login_manager.anonymous_user = Anonymous
login_manager.anonymous_user = Anonymous
From this it was able to fetch the username if it was anonymous request.
如果是匿名请求,它可以从中获取用户名。
回答by Joe Doherty
AnonymousUserMixin has no username attribute. You need to overwrite the object and call the mixin. Have a look at LoginManager.anonymous_user which is an object which is used when no user is logged in.
AnonymousUserMixin 没有用户名属性。您需要覆盖该对象并调用mixin。看看 LoginManager.anonymous_user 这是一个在没有用户登录时使用的对象。
You also need to get the user from somewhere. There is no point in storing the username in g
as you could just use current_user.username.
您还需要从某个地方获取用户。存储用户名是没有意义的,g
因为您可以只使用 current_user.username。
If you wanted to get the username you would need too
如果您想获得用户名,您也需要
if current_user.is_authenticated():
g.user = current_user.username
This would require that the user object has a property called username. There are lots of ways to customize Flask-Logins use
这将要求用户对象具有名为 username 的属性。有很多方法可以自定义 Flask-Logins 的使用
I suggest re-reading the docs and taking a look at the source code:
我建议重新阅读文档并查看源代码:
https://github.com/maxcountryman/flask-login/blob/master/flask_login.py
https://github.com/maxcountryman/flask-login/blob/master/flask_login.py
Joe
乔
回答by pjnola
Well, the error message says it all. There is no logged in user, so current_user
returns an AnonymousUserMixin
. AnonymousUserMixin
implements the interface described here: http://flask-login.readthedocs.org/en/latest/#your-user-class(which does not include a username
property). Try something like this:
好吧,错误消息说明了一切。没有登录用户,因此current_user
返回一个AnonymousUserMixin
. AnonymousUserMixin
实现这里描述的接口:http: //flask-login.readthedocs.org/en/latest/#your-user-class(不包括username
属性)。尝试这样的事情:
@pot.before_request
def load_users():
if current_user.is_authenticated():
g.user = current_user.get_id() # return username in get_id()
else:
g.user = None # or 'some fake value', whatever
Obviously, the rest of your code has to deal with the possibility that g.user
will not refer to a real user.
显然,您的其余代码必须处理g.user
不会引用真实用户的可能性。