之前已经解决了:
[已解决]Flask-Login出错:NameError: global name ‘current_user’ is not defined
但是此处又出现:
Traceback (most recent call last): File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 135, in handle self.handle_request(listener, req, client, addr) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 176, in handle_request respiter = self.wsgi(environ, resp.start_response) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__ return self.wsgi_app(environ, start_response) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app response = self.make_response(self.handle_exception(e)) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception reraise(exc_type, exc_value, tb) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app response = self.full_dispatch_request() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request rv = self.handle_user_exception(e) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception reraise(exc_type, exc_value, tb) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request rv = self.dispatch_request() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File “/root/html/SIPEvents/sipevents/views.py”, line 702, in decorated_function return f(*args, **kwargs) File “/root/html/SIPEvents/sipevents/views.py”, line 809, in index filteredUserEvents = filterUserEvents(curUser.openid) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/werkzeug/local.py”, line 343, in __getattr__ return getattr(self._get_current_object(), name) AttributeError: ‘AnonymousUserMixin’ object has no attribute ‘openid’ |
回头去查log,发现是:
g=<flask.g of ‘sipevents’>, curUser=<flask_login.AnonymousUserMixin object at 0x7f57e0f3d410> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/root/html/SIPEvents/sipevents/views.py:775]: curUser=<flask_login.AnonymousUserMixin object at 0x7f57e0f3d410> |
AttributeError: ‘AnonymousUserMixin’ object has no attribute ‘openid’
AttributeError AnonymousUserMixin object has no attribute
AttributeError: ‘AnonymousUserMixin’ object has no attribute ‘username’ | Treehouse Community
python – How to track the current user in flask-login? – Stack Overflow
-》都在说:
如何继承AnonymousUserMixin
去添加对应的属性
-》但是我此处是:
看来没有实现:
限定登录后才允许访问对应的路径啊。。
@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 |
-》
此处
g.user = current_user.get_id()
只赋值了id,但是也没有获得对应的对象啊。。。
Werkzeug Internal Server Error
AttributeError: ‘AnonymousUserMixin’ object has no attribute ‘username’ | Treehouse Community
flask网站’AnonymousUser’对象没有’confirmed’属性 – SegmentFault
去改为:
@app.before_request def before_request(): “”” 这里是全局的方法,在请求开始之前调用。 其中 flask 有个全局的变量 g,它是和 session 一样的用途,可以使用它来保存当前用户的数据 Returns: “”” if current_user.is_authenticated(): g.user = current_user else: g.user = None gLog.debug(“g=%s current_user=%s”, g, current_user) pass |
结果:
if current_user.is_authenticated(): TypeError: ‘bool’ object is not callable |
对应的:
/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/models.py
class User(db.Model): __tablename__ = ‘wechat_users’ 。。。 # is_authenticated 允许用户验证,只返回True def is_authenticated(self): return True |
所以去改为:
if current_user.is_authenticated: g.user = current_user else: g.user = None |
结果:
就可以了:
另外:
[已解决]Flask-Login中找不到next_is_valid
但是又出现另外的:
[已解决]Flask-Login出错:AttributeError Flask object has no attribute flash
至此,终于解决了。
[总结]
此处之所以出现:
AttributeError: ‘AnonymousUserMixin’ object has no attribute ‘openid’
的错误,原因是:
在before_request中,之前是直接赋值的:
g.user = current_user
而对于默认的,没有登陆的情况下,是匿名用户,此时current_user是AnonymousUserMixin类型
(而不是我们自己此处定义的,有对应的openid的属性的,User类型)
所以后面对于g.user,只要调用到User的属性和方法,就会出错。
解决办法是:
改为:
@app.before_request def before_request(): “”” 这里是全局的方法,在请求开始之前调用。 其中 flask 有个全局的变量 g,它是和 session 一样的用途,可以使用它来保存当前用户的数据 Returns: “”” if current_user.is_authenticated: g.user = current_user else: g.user = None gLog.debug(“g=%s, g.user=%s, current_user=%s”, g, g.user, current_user) pass |
注:
此处,正常的,登录后的用户,输出是:
g=<flask.g of ‘sipevents’>, g.user=<User nickname=u’crifan.com’ openid=oswjmv-QiQp-_5pFB0thKxfCZ8Tc avatar_static_path=img/avatar/oswjmv-QiQp-_5pFB0thKxfCZ8Tc.png>, current_user=<User nickname=u’crifan.com’ openid=oswjmv-QiQp-_5pFB0thKxfCZ8Tc avatar_static_path=img/avatar/oswjmv-QiQp-_5pFB0thKxfCZ8Tc.png> |
这样的话,就可以保证:
对于没有登录的current_user,类型为AnonymousUserMixin的匿名用户,是没有authenticated的,所以current_user.is_authenticated为false,所以此时g.user设置为None
这样,对于后续的:
def login_required(f): @wraps(f) def decorated_function(*args, **kwargs): if g.user is None: return redirect(url_for(‘login’, next=request.url)) return f(*args, **kwargs) return decorated_function |
login_required中,发现g.user为None,就会跳转到登录的整套逻辑里面去,
对于登录的整套逻辑中,登录完成后,会执行到:
login_user
这样,current_user.is_authenticated就为True,就可以把记录下来的,已登录的User类型的用户,赋值给g.user了,
之后,就可以正常使用User类型的g.user了,就不会再报错了。
转载请注明:在路上 » [已解决]Flask-Login出错:AttributeError AnonymousUserMixin object has no attribute openid