最新消息:20181230 VPS服务器已从Linode换到腾讯云香港,主题仍用朋友推荐的大前端D8

[已解决]Flask-Login运行出错:AttributeError _AppCtxGlobals object has no attribute user

Flask crifan 910浏览 0评论

折腾:

[记录]如何在Flask中实现记住微信公众号的用户帐户的登录状态

期间,用代码:

@app.before_request
def before_request():
    “””
    这里是全局的方法,在请求开始之前调用。
    其中 flask 有个全局的变量 g,它是和 session 一样的用途,可以使用它来保存当前用户的数据
    Returns:
    “””
    gLog.debug(“g=%s”, g)
    pass
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

结果运行出错:

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:368]:
g=<flask.g of ‘sipevents’>

<div–<——————————————————————————

[2016-09-01 17:57:47 +0000] [11963] [ERROR] Error handling request /
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 732, in decorated_function
    if g.user is None:
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/werkzeug/local.py”, line 343, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: ‘_AppCtxGlobals’ object has no attribute ‘user’

搜:

AttributeError: ‘_AppCtxGlobals’ object has no attribute ‘user’

python – AttributeError: ‘_AppCtxGlobals’ object has no attribute ‘user’ in Flask – Stack Overflow

@app.before_request
def before_request():
    g.user = current_user

python – AttributeError: ‘_AppCtxGlobals’ object has no attribute ‘user’ in flask using g – Stack Overflow

python – AttributeError: ‘_AppCtxGlobals’ object has no attribute ‘db’ – Stack Overflow

python – AttributeError: ‘_RequestGlobals’ object has no attribute ‘user’ – Stack Overflow

“The g globals are per request, not per server. The next request g is empty again.”

flask.g – Application Globals

“Starting with Flask 0.10 this is stored on the application context and no longer on the request context which means it becomes available if only the application context is bound and not yet a request.

user = getattr(flask.g, ‘user’, None)

user = flask.g.get(‘user’, None)”

看 flask 教程中遇到的问题(二) – 学习空间 – 博客频道 – CSDN.NET

【已解决】跟着做flask小博客项目报错:AttributeError: ‘_AppCtxGlobals’ object has no attribute ‘db’ – 实验楼问答 – 实验楼

flask – AttributeError: ‘User’ object has no attribute ‘is_active’ – Stack Overflow – Recalll

https://recalll.co/app/?q=flask%20-%20AttributeError%3A%20’User’%20object%20has%20no%20attribute%20’is_active’%20-%20Stack%20Overflow

[已解决]Flask-Login出错:NameError: global name ‘current_user’ is not defined

[总结]

此处在login_required中使用:

if g.user is None:

出错:

AttributeError: ‘_AppCtxGlobals’ object has no attribute ‘user’

的原因是:

此处,g中没有user这个属性

根本原因在于:

没有最开始去赋值

所以解决办法就是:

在开始用之前就赋值

而此处有个全局的before_request

把对应的赋值代码加入到那里,即可:

@app.before_request
def before_request():
    “””
    这里是全局的方法,在请求开始之前调用。
    其中 flask 有个全局的变量 g,它是和 session 一样的用途,可以使用它来保存当前用户的数据
    Returns:
    “””
    g.user = current_user
    gLog.debug(“g=%s”, g)
    pass

-》

这样每次调用login_required之前就会通过before_request获取到对应的current_user给g.user

之后在login_required,以及其他路由router中,就可以使用g.user了。

注:

如果是未来避免崩溃出错,也可以用get去获取:

user = g.get(‘user’, None)

或:

user = getattr(g, ‘user’, None)

转载请注明:在路上 » [已解决]Flask-Login运行出错:AttributeError _AppCtxGlobals object has no attribute user

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
67 queries in 0.071 seconds, using 9.41MB memory