title: Rendering template gives "jinja2.exceptions.UndefinedError: 'form' is undefined"
body: <p>I am working through Miguel Grinberg's Flask Mega Tutorial and I cannot figure out why the index page now fails to load.  Here is the traceback:</p>
  <pre><code>  File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py&quot;, line 1836, in __call__
      return self.wsgi_app(environ, start_response)
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py&quot;, line 1820, in wsgi_app
      response = self.make_response(self.handle_exception(e))
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py&quot;, line 1403, in handle_exception
      reraise(exc_type, exc_value, tb)
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py&quot;, line 1817, in wsgi_app
      response = self.full_dispatch_request()
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py&quot;, line 1477, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py&quot;, line 1381, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py&quot;, line 1475, in full_dispatch_request
      rv = self.dispatch_request()
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py&quot;, line 1461, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask_login.py&quot;, line 658, in decorated_view
      return func(*args, **kwargs)
    File &quot;/home/asdoylejr/microblog/app/views.py&quot;, line 44, in index
      posts = posts)
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/templating.py&quot;, line 128, in render_template
      context, ctx.app)
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/templating.py&quot;, line 110, in _render
      rv = template.render(context)
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/jinja2/environment.py&quot;, line 969, in render
      return self.environment.handle_exception(exc_info, True)
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/jinja2/environment.py&quot;, line 742, in handle_exception
      reraise(exc_type, exc_value, tb)
    File &quot;/home/asdoylejr/microblog/app/templates/index.html&quot;, line 2, in top-level template code
      {% extends &quot;base.html&quot; %}
    File &quot;/home/asdoylejr/microblog/app/templates/base.html&quot;, line 30, in top-level template code
      {% block content %}{% endblock %}
    File &quot;/home/asdoylejr/microblog/app/templates/index.html&quot;, line 7, in block &quot;content&quot;
      {{form.hidden_tag()}}
    File &quot;/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/jinja2/environment.py&quot;, line 397, in getattr
      return getattr(obj, attribute)
  UndefinedError: 'form' is undefined
  </code></pre>
  <p>Here is the code for the index page in question:</p>
  <pre><code>&lt;!-- extend base layout --&gt;
  {% extends &quot;base.html&quot; %}
  
  {% block content %}
  &lt;h1&gt;Hi, {{g.user.nickname}}!&lt;/h1&gt;
  &lt;form action=&quot;&quot; method=&quot;post&quot; name=&quot;post&quot;&gt;
      {{form.hidden_tag()}}
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;Say something:&lt;/td&gt;
          &lt;td&gt;{{form.post(size = 30, maxlength = 140)}}&lt;/td&gt;
          &lt;td&gt;
          {% for error in form.errors.post %}
          &lt;span style=&quot;color: red;&quot;&gt;[{{error}}]&lt;/span&gt;&lt;br&gt;
          {% endfor %}
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Post!&quot;&gt;&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  {% for post in posts %}
  &lt;p&gt;
      {{post.author.nickname}} says: &lt;b&gt;{{post.body}}&lt;/b&gt;
  &lt;/p&gt;
  {% endfor %}
  {% endblock %}
  </code></pre>
  <p>And here is the code in the view:</p>
  <pre><code>@app.route('/', methods = ['GET', 'POST'])
  @app.route('/index', methods = ['GET', 'POST'])
  @login_required
  def index():
      form = PostForm()
      if form.validate_on_submit():
          post = Post(body = form.post.data, timestamp = datetime.utcnow(), author = g.user)
          db.session.add(post)
          db.session.commit()
          flash('Your post is now live!')
          return redirect(url_for('index'))
      posts = g.user.followed_posts().all()
      return render_template(&quot;index.html&quot;,
          title = 'Home',
          user = user,
          posts = posts)
  </code></pre>
  <p>I've read through the tutorial multiple times, and compared my code to the source at the end of each lesson, and I have no idea why it is not working.  I'm not sure why it's having trouble passing form in this view when it does not return an error passing forms in another view.</p>
  <p>Can anyone point me in the right direction?</p>