【问题标题】:BuildError while using routing with variables使用带变量的路由时出现BuildError
【发布时间】:2016-10-12 02:15:41
【问题描述】:
from flask import Flask, render_template

app = Flask(__name__)
@app.route('/')
def home():
    return render_template("home.html")

@app.route('/about/<name>')
def about(name):
    return render_template('about.html')

if __name__ == '__main__':
    app.run(debug=True)

当我去:http://localhost:5000/about/abc

这段代码给了我:

BuildError:无法为端点“about”构建 url。您是否忘记指定值 ['name']?

我做错了什么?

解决方案是在 about.html 继承的 layout.html 中添加 name=name。我不明白这里发生了什么。

<li><a href="{{ url_for('about', name=name) }}">About</a></li>

【问题讨论】:

    标签: web-applications flask flask-wtforms web-frameworks flask-restful


    【解决方案1】:

    听起来您未能在模板文件夹中创建 about.html。确保该文件夹称为模板,并且它与您的 python 代码位于同一目录中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      相关资源
      最近更新 更多