AttributeError:“_AppCtxGlobals”对象在 Flask 中没有属性“用户”
Posted
技术标签:
【中文标题】AttributeError:“_AppCtxGlobals”对象在 Flask 中没有属性“用户”【英文标题】:AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask 【发布时间】:2014-02-03 22:58:55 【问题描述】:我正在尝试通过关注Flask Mega Tutorial 来学习烧瓶。在part 5 中,login() 视图是这样编辑的:
@app.route('/login', methods = ['GET', 'POST'])
@oid.loginhandler
def login():
if g.user is not None and g.user.is_authenticated():
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
session['remember_me'] = form.remember_me.data
return oid.try_login(form.openid.data, ask_for = ['nickname', 'email'])
return render_template('login.html',
title = 'Sign In',
form = form,
providers = app.config['OPENID_PROVIDERS'])
但是,这给了我一个 AttributeError,我将在下面粘贴 StackTrace。它给出了一个错误,我从示例的源代码中完全粘贴了该错误。我确实使用PeeWee 而不是 SQLAlchemy,但由于这段代码对数据库没有任何作用,但我不知道为什么会这样。
有人知道我在这里做错了什么吗?
Traceback (most recent call last):
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/flask_openid.py", line 446, in decorated
return f(*args, **kwargs)
File "/Users/kramer65/dev/repos/microblog/app/views.py", line 31, in login
if g.user is not None and g.user.is_authenticated():
File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
AttributeError: '_AppCtxGlobals' object has no attribute 'user'
【问题讨论】:
【参考方案1】:同样的教程,更进一步,解释了如何设置g.user
:
g.user 全局
如果你注意了,你会记得在登录视图函数中,我们检查
g.user
以确定用户是否已经登录。为了实现这一点,我们将使用来自 Flask 的before_request
事件。每次收到请求时,任何用before_request
修饰的函数都将在视图函数之前运行。所以这是设置我们的g.user
变量(文件app/views.py
)的正确位置:@app.before_request def before_request(): g.user = current_user
这就是它所需要的。
current_user
全局由 Flask-Login 设置, 所以我们只需在g
对象中放置一个副本,以便更好地访问它。 有了这个,所有请求都可以访问登录用户,即使 在模板内。
您的代码显然缺少这个 before_request
处理程序。
【讨论】:
以上是关于AttributeError:“_AppCtxGlobals”对象在 Flask 中没有属性“用户”的主要内容,如果未能解决你的问题,请参考以下文章
AttributeError: 'RDD' 对象没有属性 'show'
AttributeError:“NumpyArrayIterator”对象没有属性“类”
AttributeError:模块 'dbus' 没有属性 'lowlevel'