title: Unable to use flask.g to access variables in other functions
body: <p>I'm trying to use <code>flask.g</code> to store variables that can be accessed in other functions, but I don't seem to be doing something correctly.  The application generates the following error when I try to access <code>g.name</code>:  <code>AttributeError: '_RequestGlobals' object has no attribute 'name'</code>.</p>
  
  <p>The <a href="http://flask.pocoo.org/docs/api/#flask.g" rel="noreferrer">documentation</a> for <code>flask.g</code> says:</p>
  
  <blockquote>
    <p>Just store on this whatever you want. For example a database
    connection or the user that is currently logged in.</p>
  </blockquote>
  
  <p>Here's a complete, minimal example that illustrates the error that I receive when trying to access the variable outside of the function it was created in.  Any help would be greatly appreciated.</p>
  
  <pre><code>#!/usr/bin/env python
  
  from flask import Flask, render_template_string, request, redirect, url_for, g
  from wtforms import Form, TextField
  
  application = app = Flask('wsgi')
  
  @app.route('/', methods=['GET', 'POST'])
  def index():
      form = LoginForm(request.form)
  
      if request.method == 'POST' and form.validate():
          name =  form.name.data
          g.name = name
  
          # Need to create an instance of a class and access that in another route
          #g.api = CustomApi(name)
  
          return redirect(url_for('get_posts'))
  
      else:
  
          return render_template_string(template_form, form=form)
  
  @app.route('/posts', methods=['GET'])
  def get_posts():
      # Need to access the instance of CustomApi here
      #api = g.api
      name = g.name
      return render_template_string(name_template, name=name)
  
  
  class LoginForm(Form):
      name = TextField('Name')
  
  template_form = """
  {% block content %}
  &lt;h1&gt;Enter your name&lt;/h1&gt;
  
  &lt;form method="POST" action="/"&gt;
      &lt;div&gt;{{ form.name.label }} {{ form.name() }}&lt;/div&gt;&lt;br&gt;
      &lt;button type="submit" class="btn"&gt;Submit&lt;/button&gt;    
  &lt;/form&gt;
  {% endblock %}
  
  """
  
  name_template = """
  {% block content %}
  
      &lt;div&gt;"Hello {{ name }}"&lt;/div&gt;&lt;br&gt;
  
  {% endblock %}
  
  """
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>