As far as I understand the g variable in Flask, it should provide me with a global place to store data, such as saving the current user after logging in. Is it correct?
I would like my navigation to display my username after logging in through the site.
My views contain
from Flask import g
At login, I assign
user = User.query.filter_by(username = form.username.data).first() if validate(user): session['logged_in'] = True g.user = user
It seems I can not access g.user. Instead, when my base.html template has the following ...
<ul class="nav"> {% if session['logged_in'] %} <li class="inactive">logged in as {{ g.user.username }}</li> {% endif %} </ul>
I get an error message:
jinja2.exceptions.UndefinedError UndefinedError: 'flask.ctx._RequestGlobals object' has no attribute 'user'
Otherwise, the login works fine. What am I missing?
python flask jinja2 flask-login
Mittenchops Nov 29 '12 at 1:12 2012-11-29 01:12
source share