【问题标题】:Flask - UndefinedError: 'restaurant_id' is undefinedFlask - UndefinedError: 'restaurant_id' 未定义
【发布时间】: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>
            &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>

这是我管理路线的网络服务器:

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


【解决方案1】:

您不会在 jinja 模板中向restaurant_id 发送任何内容:

{{url_for('newRestaurant', restaurant_id = restaurant_id)}}

第二个restaurant_id 未定义,因为您没有通过它向模板发送任何值。

您应该通过newRestaurant 函数发送一个号码。

例如(这只是一个简单的示例),您可以验证 Restaurant 表中是否没有行,如果没有,则发起 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 变量向模板发送一个数字,您可以在那里使用它。

【讨论】:

  • 我已经仔细听从了您的建议并理解了。非常感谢。执行后,我仍然得到同样的错误(UndefinedError: 'restaurant_id' is undefined)
  • 已编辑:你写了POS 而不是POST,我从路线中删除了/new。尝试一下。您还应该将模板页面的代码与发送POST的表单一起放置。
  • 渲染模板的函数是render_template而不是render
  • 对不起,我不明白你所说的“将模板页面的代码与发送 POST 的表单一起”的意思。
  • POST 请求通常通过其method 属性从表单发送。你应该阅读HTTP methods
【解决方案2】:

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

鉴于我只需要创建一家新餐厅,我不需要在 url 上捕获餐厅的 id。所以改为像这样路由

@app.route('/restaurant//new', methods = ['GET','POST'])

我删除了 id 部分并像这样删除了 def newRestaurant 函数中的参数

@app.route('/restaurant/new', methods = ['GET','POST'])

非常感谢大家提供线索

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 2015-01-18
      • 2015-08-14
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      相关资源
      最近更新 更多