Q:

jinja2.exceptions.UndefinedError How to define 'restaurant'

as the title says I'm getting an error code when I attempt render one of my website pages. 
Here are the parts of the traceback that are pointing to my code:
  File "/vagrant/catalog/project.py", line 48, in newMenuItem
    return render_template('newMenuItem.html', restaurant_id=restaurant.id)

  File "/vagrant/catalog/templates/newMenuItem.html", line 9, in block "content"
    <form action="{{ url_for('newMenuItem', restaurant_id=restaurant.id) }}" method="POST">
  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 397, in getattr
    return getattr(obj, attribute)
UndefinedError: 'restaurant' is undefined

As you can see it is telling me that 'restaurant' is not defined, at first I thought perhaps I wasn't querying my database correctly so I double checked that section of my code and I can't find any problems.
Here is my python module and relevant html file:
### Create new menu item. ###
@app.route('/restaurant/<int:restaurant_id>/menu/new/',
            methods=['GET', 'POST'])
def newMenuItem(restaurant_id):
    restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
    if request.method == 'POST':
        newMenuItem = MenuItem(name=request.form['name'],
                               description=request.form['description'],
                               price=request.form['price'],
                               restaurant_id=restaurant.id)
        session.add(newMenuItem)
        session.commit()
        return redirect(url_for('showMenu', restaurant_id=restaurant.id))
    else:
        return render_template('newMenuItem.html', restaurant_id=restaurant.id)

and here is the newMenuItem html file:
{% extends "base.html" %}
{% block content %}
<div class="twelve columns">
  New Item.
</div>

<div class="row">
  <div class="six columns">
    <form action="{{ url_for('newMenuItem', restaurant_id=restaurant.id) }}" method="POST">
      <div class="row">
        <div class="six columns">
          <label for="Item Name">Item Name</label>
          <input class="u-full-width" placeholder="New Item" name="name" type="text">
        </div>
        <div class="six columns">
          <label for="Item Name">Item Description</label>
          <input class="u-full-width" placeholder="Description" description="description" type="text">
        </div>
        <div class="six columns">
          <label for="Item Name">Item Price</label>
          <input class="u-full-width" placeholder="Price" price="price" type="text">
        </div>
        <div class="six columns">
          <input class="button" value="Create" type="submit">
          <a class="button" href="{{ url_for('showMenu', restaurant_id=restaurant.id) }}">Cancel</a>
        </div>
      </div>
    </form>
  </div>
</div>
{% endblock %}

I am having trouble understanding why this error is being thrown with this specific page, as I have used similar url_for code to create a new restaurant. The only thing I could think of that would be causing this issue is that my template cannot figure out the restaurant id where the new menu item should be created. I am unsure of how to read this error code as I do not know whether it is telling me that 'restaurant' as a variable is undefined, or that 'restaurant' is undefined in the sense that my code cannot call the correct id.
Any suggestions? Thanks in advance for any help offered.

A:

There is no restaurant variable passed into the template context, there is only restaurant_id in the context. Replace:
return render_template('newMenuItem.html', restaurant_id=restaurant.id)

with:
return render_template('newMenuItem.html', restaurant=restaurant)

Or, if you need only the restaurant ID in the template, leave the rendering part as is and access the id via restaurant_id instead of restaurant.id.

