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\'
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.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.
Your code is apparently missing this before_request handler.