Flask - UndefinedError: 'restaurant_id' is undefined
Flask - UndefinedError: 'restaurant_id' is undefined
我尝试从该页面引用另一个页面
<html>
<head>
</head>
<body>
<header>
<h1>Restaurants<h1>
</header>
{% for i in restaurants %}
<h5>
<a href= "*">
{{i.name}}
</a>
   
<a href = "*"> Edit </a>  
<a href = "*"> Delete </a>
</h5>
{% endfor %}
<a href = {{url_for('newRestaurant', restaurant_id = restaurant_id)}}> Create new restaurant </a>
</body>
</html>
这是我管理路由的网络服务器:
from flask import Flask, render_template, request, redirect, url_for, flash
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, MenuItem
app = Flask(__name__)
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
session = DBSession()
# Show all restaurants
.....
#Create new restaurants
@app.route('/restaurant/<int:restaurant_id>/new', methods = ['GET','POS'])
def newRestaurant(restaurant_id):
if request.method == 'POST':
new_Restaurant = Restaurant(name = request.form['new_restaurant_name'], restaurant_id = restaurant_id)
session.add(new_Restaurant)
session.commit()
#return "This page will be for making a new restaurant"
flash("new restaurant create")
return redirect(url_for('restaurants', restaurant_id = restaurant_id))
else:
return render('newRestaurant.html', restaurant_id = restaurant_id)
我在这里尝试路由到我创建的餐厅页面:
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)
您没有在 jinja 模板中向 restaurant_id
发送任何内容:
{{url_for('newRestaurant', restaurant_id = restaurant_id)}}
第二个 restaurant_id
未定义,因为您没有通过它向模板发送任何值。
您应该从 newRestaurant
函数中发送一个号码。
例如(这只是一个简单的例子),您可以验证 Restaurant table 中是否没有行,如果不是,则启动 restaurant_id = 1
:
已编辑
@app.route('/restaurant/<int:restaurant_id>', methods = ['GET','POST'])
def newRestaurant(restaurant_id):
if request.method == 'POST':
.............
else:
restaurants = session.query(Restaurant).all()
if not restaurants:
restaurant_id = 1
else:
nr_restaurants = session.query(Restaurant).count()
restaurant_id = int(nr_restaurants) + 1
return render_template('newRestaurant.html', restaurant_id = restaurant_id)
现在您正在通过 restaurant_id
变量向模板发送一个数字,您可以在那里使用它。
我解决了这个问题,下面是
考虑到我只需要创建一家新餐厅,我不需要在 url 上捕获餐厅的 ID。所以改为像这样路由
@app.route('/restaurant//new', methods = ['GET','POST'])
我像这样删除了 id 部分并删除了 def newRestaurant 函数中的参数
@app.route('/restaurant/new', 方法 = ['GET','POST'])
非常感谢大家提供的线索
当我在我的 deleteMenuItem
定义下添加 restaurant_id=restaurant_id
时它起作用了。
我尝试从该页面引用另一个页面
<html>
<head>
</head>
<body>
<header>
<h1>Restaurants<h1>
</header>
{% for i in restaurants %}
<h5>
<a href= "*">
{{i.name}}
</a>
   
<a href = "*"> Edit </a>  
<a href = "*"> Delete </a>
</h5>
{% endfor %}
<a href = {{url_for('newRestaurant', restaurant_id = restaurant_id)}}> Create new restaurant </a>
</body>
</html>
这是我管理路由的网络服务器:
from flask import Flask, render_template, request, redirect, url_for, flash
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, MenuItem
app = Flask(__name__)
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
session = DBSession()
# Show all restaurants
.....
#Create new restaurants
@app.route('/restaurant/<int:restaurant_id>/new', methods = ['GET','POS'])
def newRestaurant(restaurant_id):
if request.method == 'POST':
new_Restaurant = Restaurant(name = request.form['new_restaurant_name'], restaurant_id = restaurant_id)
session.add(new_Restaurant)
session.commit()
#return "This page will be for making a new restaurant"
flash("new restaurant create")
return redirect(url_for('restaurants', restaurant_id = restaurant_id))
else:
return render('newRestaurant.html', restaurant_id = restaurant_id)
我在这里尝试路由到我创建的餐厅页面:
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)
您没有在 jinja 模板中向 restaurant_id
发送任何内容:
{{url_for('newRestaurant', restaurant_id = restaurant_id)}}
第二个 restaurant_id
未定义,因为您没有通过它向模板发送任何值。
您应该从 newRestaurant
函数中发送一个号码。
例如(这只是一个简单的例子),您可以验证 Restaurant table 中是否没有行,如果不是,则启动 restaurant_id = 1
:
已编辑
@app.route('/restaurant/<int:restaurant_id>', methods = ['GET','POST'])
def newRestaurant(restaurant_id):
if request.method == 'POST':
.............
else:
restaurants = session.query(Restaurant).all()
if not restaurants:
restaurant_id = 1
else:
nr_restaurants = session.query(Restaurant).count()
restaurant_id = int(nr_restaurants) + 1
return render_template('newRestaurant.html', restaurant_id = restaurant_id)
现在您正在通过 restaurant_id
变量向模板发送一个数字,您可以在那里使用它。
我解决了这个问题,下面是
考虑到我只需要创建一家新餐厅,我不需要在 url 上捕获餐厅的 ID。所以改为像这样路由
@app.route('/restaurant//new', methods = ['GET','POST'])
我像这样删除了 id 部分并删除了 def newRestaurant 函数中的参数
@app.route('/restaurant/new', 方法 = ['GET','POST'])
非常感谢大家提供的线索
当我在我的 deleteMenuItem
定义下添加 restaurant_id=restaurant_id
时它起作用了。