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'])
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'
The same tutorial, a little further on, explains how g.user
is set:
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 thebefore_request
event from Flask. Any functions that are decorated withbefore_request
will run before the view function each time a request is received. So this is the right place to setup ourg.user
variable (fileapp/views.py
):@app.before_request def before_request(): g.user = current_user
This is all it takes. The
current_user
global is set by Flask-Login, so we just put a copy in theg
object to have better access to it. With this, all requests will have access to the logged in user, even inside templates.
Your code is apparently missing this before_request
handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With