【发布时间】:2015-06-30 09:51:51
【问题描述】:
我尝试从此页面引用另一个页面
<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)
【问题讨论】:
-
请尝试正确设置格式(键入时检查预览区域)。如果您的帖子难以阅读,您将无法获得答案。我现在会为你解决这个问题。
-
您能否提供重新创建错误的步骤。据我了解,Jinja2 模板的视图不知道 restaurant_id 是什么
-
为此我需要粘贴整个 webserver.py,你想让我怎么把它发给你,或者你有办法让我分享它
标签: python flask url-routing function