AttributeError:"_ AppCtxGlobals"对象在Flask中没有属性"user" [英] AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask

查看:282
本文介绍了AttributeError:"_ AppCtxGlobals"对象在Flask中没有属性"user"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过遵循 Flask Mega教程来学习Flask .在第5部分中,login()视图的编辑方式类似于所以:

I'm trying to learn flask by following the Flask Mega Tutorial. In part 5, the login() view is edit like so:

@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,但是由于这段代码对数据库没有任何作用我不知道为什么会这样.

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 PeeWee instead 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.

有人知道我在这里做错了吗?

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'

推荐答案

同一教程(进一步介绍)说明了如何设置g.user:

The same tutorial, a little further on, explains how g.user is set:

全局g.user

如果引起注意,您会记得在登录视图功能中我们检查了g.user以确定用户是否已经登录.要实现此目的,我们将使用Flask中的before_request事件.每次接收到请求时,任何用before_request装饰的功能都将在查看功能之前运行.因此,这是设置g.user变量(文件app/views.py)的正确位置:

The g.user global

If you were paying attention, you will remember that in the login view function we check g.user to determine if a user is already logged in. To implement this we will use the before_request event from Flask. Any functions that are decorated with before_request will run before the view function each time a request is received. So this is the right place to setup our g.user variable (file app/views.py):

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

这就是全部. current_user全局由Flask-Login设置, 因此我们只是将副本放在g对象中,以便更好地访问它. 这样,所有请求都可以访问已登录的用户,即使是 内部模板.

This is all it takes. The current_user global is set by Flask-Login, so we just put a copy in the g object to have better access to it. With this, all requests will have access to the logged in user, even inside templates.

您的代码显然缺少此before_request处理程序.

Your code is apparently missing this before_request handler.

这篇关于AttributeError:"_ AppCtxGlobals"对象在Flask中没有属性"user"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆