title: How to use g.user global in flask
body: <p>As I understand the g variable in Flask, it should provide me with a global place to stash data like holding the current user after login.  Is this correct?</p>
  
  <p>I would like my navigation to display my user's name, once logged in, across the site.</p>
  
  <p>My views contain</p>
  
  <pre><code>from Flask import g #among other things
  </code></pre>
  
  <p>During login, I assign </p>
  
  <pre><code>user = User.query.filter_by(username = form.username.data).first()
  if validate(user):
      session['logged_in'] = True
      g.user = user
  </code></pre>
  
  <p>It doesn't seem I can access g.user.  Instead, when my base.html template has the following...</p>
  
  <pre><code>&lt;ul class="nav"&gt;
      {% if session['logged_in'] %}
          &lt;li class="inactive"&gt;logged in as {{ g.user.username }}&lt;/li&gt;
      {% endif %}
  &lt;/ul&gt;
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>jinja2.exceptions.UndefinedError
  UndefinedError: 'flask.ctx._RequestGlobals object' has no attribute 'user'
  </code></pre>
  
  <p>The login otherwise works fine.  What am I missing?</p>