UnboundLocalError: local variable 'restaurantToDelete' referenced before assignment Flask app
<p>When I try to delete an item from the database in a Flask view, the following error is shown</p>

<pre><code>UnboundLocalError: local variable 'restaurantToDelete' referenced before assignment
</code></pre>



<pre><code>@app.route('/restaurant/&lt;int:restaurant_id&gt;/delete',methods=['GET','POST'])
def deleteRestaurant(restaurant_id):
    if request.method=='POST':
        restaurantToDelete=session.query(Restaurant).filter_by(id=restaurant_id).one()
        session.delete(restaurantToDelete)
        session.commit()
        return redirect(url_for('showRestaurants'))
    else:
        return render_template('deleterestaurant.html',restaurant=restaurantToDelete)
</code></pre>

<p>Look at the else, at that point restaurantToDelete isn't defined, your code should be something like</p>

<pre><code>@app.route('/restaurant/&lt;int:restaurant_id&gt;/delete',methods=['GET','POST'])
def deleteRestaurant(restaurant_id):
    restaurantToDelete=session.query(Restaurant).filter_by(id=restaurant_id).one()

    if request.method=='POST':
        session.delete(restaurantToDelete)
        session.commit()
        return redirect(url_for('showRestaurants'))
    else:
        return render_template('deleterestaurant.html',restaurant=restaurantToDelete)
</code></pre>

python flask sqlalchemy