Flask-UndefinedError:'restaurant_id'未定义

问题描述 投票:1回答:3

我尝试引用此页面的另一页

<html>
    <head>
    </head>
    <body>
          <header>
              <h1>Restaurants<h1>
          </header>


          {% for i in restaurants %}

          <h5>
              <a href= "*">
                  {{i.name}}
              </a>
            &nbsp &nbsp
              <a href = "*">  Edit </a> &nbsp
              <a href = "*">  Delete  </a>
          </h5>

           {% endfor %}

        <a href = {{url_for('newRestaurant', restaurant_id = restaurant_id)}}> Create new restaurant </a>
    </body>
</html>

这是我管理路由的Web服务器:

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)
python flask url-routing function
3个回答
1
投票

您不向Jinja模板中的restaurant_id发送任何内容:


0
投票

我解决了问题,这是如何解决


0
投票

当我在restaurant_id=restaurant_id定义下添加deleteMenuItem时,它起作用。

© www.soinside.com 2019 - 2024. All rights reserved.