AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask


Cramer 65

I'm trying to learn Flask following the Flask Mega Tutorial . In part 5 , the login() view is edited as follows:

@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'])

However, this gave me an AttributeError, I'll paste the StackTrace below. This is exactly the error I pasted from the example source. I do use PeeWee instead of SQLAlchemy, but since this code doesn't do anything with the database, I don't know why this is happening.

Does anyone know what I am 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'
Martijn Pieters

The tutorial goes further on how to g.userset up:

g.user global

If you are following, you will remember that in the login view function we check g.userto see if the user is already logged in. To achieve this, we will use events in before_requestFlask . before_requestEvery time a request is received , all functions decorated with functions are run before the viewing functions. So this is the correct place to set the g.uservariable (file app/views.py) :

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

this is all. The current_userglobal is set by Flask-Login, so we just put the copied gobject to have better access. This way the logged in user is accessible to all requests, even inside the template.

Your code is obviously missing this before_requesthandler.

Related


AttributeError: 'Flask' object has no attribute 'user_options'

Ravdeep I'm trying to setup this basic example with the following documentation: http://flask.pocoo.org/docs/patterns/celery/ But so far I keep getting the following error: AttributeError: 'Flask' object has no attribute 'user_options' I am using celery 3.1.15

AttributeError: 'Flask' object has no attribute 'user_options'

Ravdeep: I'm trying to setup this basic example with the following documentation: http://flask.pocoo.org/docs/patterns/celery/ But so far I still get the following error: AttributeError: 'Flask' object has no attribute 'user_options' I am using celery 3.1.15.

AttributeError: 'Flask' object has no attribute 'user_options'

Ravdeep: I'm trying to setup this basic example with the following documentation: http://flask.pocoo.org/docs/patterns/celery/ But so far I keep getting the following error: AttributeError: 'Flask' object has no attribute 'user_options' I am using celery 3.1.1

AttributeError: 'Flask' object has no attribute 'user_options'

Ravdeep: I'm trying to setup this basic example with the following documentation: http://flask.pocoo.org/docs/patterns/celery/ But so far I still get the following error: AttributeError: 'Flask' object has no attribute 'user_options' I am using celery 3.1.15.

AttributeError: 'Flask' object has no attribute 'user_options'

Ravdeep: I'm trying to setup this basic example with the following documentation: http://flask.pocoo.org/docs/patterns/celery/ But so far I keep getting the following error: AttributeError: 'Flask' object has no attribute 'user_options' I am using celery 3.1.1

AttributeError: 'Flask' object has no attribute 'user_options'

Ravdeep: I'm trying to setup this basic example with the following documentation: http://flask.pocoo.org/docs/patterns/celery/ But so far I still get the following error: AttributeError: 'Flask' object has no attribute 'user_options' I am using celery 3.1.15.

Flask - AttributeError: 'module' object has no attribute 'items'

Utopia I am using flask-restful and have the following API class: views.py from datetime import date from flask import jsonify from flask.ext.restful import Resource, reqparse from backend import db from .models import User, Post, Comment, WorkExperience from

Flask - AttributeError: 'module' object has no attribute 'items'

Utopia I am using flask-restful and have the following API class: views.py from datetime import date from flask import jsonify from flask.ext.restful import Resource, reqparse from backend import db from .models import User, Post, Comment, WorkExperience from

Flask - AttributeError: 'module' object has no attribute 'items'

Utopia I am using flask-restful and have the following API class: views.py from datetime import date from flask import jsonify from flask.ext.restful import Resource, reqparse from backend import db from .models import User, Post, Comment, WorkExperience from

Flask - AttributeError: 'module' object has no attribute 'items'

Utopia I am using flask-restful and have the following API class: views.py from datetime import date from flask import jsonify from flask.ext.restful import Resource, reqparse from backend import db from .models import User, Post, Comment, WorkExperience from

Flask AttributeError: Module object has no attribute 'app'

Chet I'm new to flask and I'm importing the relevant AttributeError below when I'm in flask runthe flask_lab folder . Any help would be greatly appreciated. Work list: flask_lab ├── __init__.py ├── Pipfile ├── Pipfile.lock ├── README.md ├── tmp │   ├── __init_

Flask - AttributeError: 'module' object has no attribute 'items'

Utopia I am using flask-restful and have the following API class: views.py from datetime import date from flask import jsonify from flask.ext.restful import Resource, reqparse from backend import db from .models import User, Post, Comment, WorkExperience from

Flask AttributeError: Module object has no attribute 'app'

Chet I'm new to flask and I'm importing the relevant AttributeError below when I'm in flask runthe flask_lab folder . Any help would be greatly appreciated. Work list: flask_lab ├── __init__.py ├── Pipfile ├── Pipfile.lock ├── README.md ├── tmp │   ├── __init_

Flask AttributeError: Module object has no attribute 'app'

Chet I'm new to flask and I'm importing the relevant AttributeError below when I'm in flask runthe flask_lab folder . Any help would be greatly appreciated. Work list: flask_lab ├── __init__.py ├── Pipfile ├── Pipfile.lock ├── README.md ├── tmp │   ├── __init_

Flask - AttributeError: 'module' object has no attribute 'items'

Utopia I am using flask-restful and have the following API class: views.py from datetime import date from flask import jsonify from flask.ext.restful import Resource, reqparse from backend import db from .models import User, Post, Comment, WorkExperience from

AttributeError: object of type 'user' has no attribute 'name'

Freddie Martinez Garcia I am new to graphene and I have this: from django.contrib.auth.models import User class UserType(DjangoObjectType): class Meta: model = User Basically, using Django's User class gives me this error because before using the

AttributeError on /notify/ 'user' object has no attribute 'get'

Ahmed Yasin When I create the user object, in views.py. It shows me and the error AttributeError at /notify/ , 'User' object has no attribute 'get', how can I fix it. from django.contrib.auth.models import User class NotifyView(TemplateView): template_nam

AttributeError: 'user' object has no attribute 'is_admin'

strontium I customized the user model by extending AbstractBaseUser which accepts only as email id. Here is the model: class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField('email address', unique=True) first_name = models.CharField

AttributeError: 'WSGIRequest' object has no attribute 'user'

Zhiyuan Leng I am running Django code and I am getting this error AttributeError: 'WSGIRequest' object has no attribute 'user' My Django version is 1.8.2 and this is my middleware class setting.py MIDDLEWARE_CLASSES = ( 'khxia.middlewares.PeeweeConnectionMid

AttributeError: type object 'user' has no attribute 'query'

Mark Herbert I have a large database, populated with sqlalchemy (original database - not flask-SQLAlachemy). I'm trying to add flask-security to a website, I realize it relies on flask-SQLAlachemy (not the original), but flask-SQLAlchemy can work with me (just

AttributeError on /notify/ 'user' object has no attribute 'get'

Ahmed Yasin When I create the user object, in views.py. It shows me and the error AttributeError at /notify/ , 'User' object has no attribute 'get', how can I fix it. from django.contrib.auth.models import User class NotifyView(TemplateView): template_nam

AttributeError: object of type 'user' has no attribute 'name'

Freddie Martinez Garcia I am new to graphene and I have this: from django.contrib.auth.models import User class UserType(DjangoObjectType): class Meta: model = User Basically, using Django's User class gives me this error because before using the

AttributeError: 'WSGIRequest' object has no attribute 'user'

Zhiyuan Leng I am running Django code and I am getting this error AttributeError: 'WSGIRequest' object has no attribute 'user' My Django version is 1.8.2 and this is my middleware class setting.py MIDDLEWARE_CLASSES = ( 'khxia.middlewares.PeeweeConnectionMid

AttributeError: 'WSGIRequest' object has no attribute 'user'

Zhiyuan Leng I am running Django code and I am getting this error AttributeError: 'WSGIRequest' object has no attribute 'user' My Django version is 1.8.2 and this is my middleware class setting.py MIDDLEWARE_CLASSES = ( 'khxia.middlewares.PeeweeConnectionMid

AttributeError: 'WSGIRequest' object has no attribute 'user'

Zhiyuan Leng I am running Django code and I am getting this error AttributeError: 'WSGIRequest' object has no attribute 'user' My Django version is 1.8.2 and this is my middleware class setting.py MIDDLEWARE_CLASSES = ( 'khxia.middlewares.PeeweeConnectionMid