Python AttributeError: '_AppCtxGlobals' 对象在 Flask 中没有属性 'user'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21138025/
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
AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask
提问by kramer65
I'm trying to learn flask by following the Flask Mega Tutorial. In part 5, the login() view is edit like so:
我正在尝试通过遵循Flask Mega 教程来学习Flask。在第 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'])
This however, gets me an AttributeError of which I'll paste the StackTrace below. It gives an error on a piece of which I pasted exactly from the source of the examples. I do use PeeWeeinstead of SQLAlchemy, but since this piece of code doesn't do anything with the DB yet I wouldn't know why that would be related.
然而,这给了我一个 AttributeError,我将在下面粘贴 StackTrace。它给出了我从示例源中完全粘贴的一段错误。我确实使用PeeWee而不是 SQLAlchemy,但由于这段代码对数据库没有任何作用,但我不知道为什么会相关。
Does anybody know what I might be doing wrong here?
有谁知道我在这里可能做错了什么?
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'
采纳答案by Martijn Pieters
The same tutorial, a little further on, explains how g.useris set:
同一个教程,更进一步,解释了如何g.user设置:
The g.user global
If you were paying attention, you will remember that in the login view function we check
g.userto determine if a user is already logged in. To implement this we will use thebefore_requestevent from Flask. Any functions that are decorated withbefore_requestwill run before the view function each time a request is received. So this is the right place to setup ourg.uservariable (fileapp/views.py):@app.before_request def before_request(): g.user = current_userThis is all it takes. The
current_userglobal is set by Flask-Login, so we just put a copy in thegobject to have better access to it. With this, all requests will have access to the logged in user, even inside templates.
g.user 全局
如果您有注意,您会记得在登录视图函数中,我们检查
g.user以确定用户是否已经登录。为了实现这一点,我们将使用before_request来自 Flask的事件。before_request每次收到请求时,任何被修饰的函数都会在视图函数之前运行。所以这是设置我们的g.user变量(文件app/views.py)的正确位置:@app.before_request def before_request(): g.user = current_user这就是全部。在
current_user全球是由瓶,登录设置,所以我们只要把复制的g对象有更好的访问。有了这个,所有请求都可以访问登录用户,甚至在模板中。
Your code is apparently missing this before_requesthandler.
您的代码显然缺少此before_request处理程序。

