这个代码为什么每次都运行的都只是第一个@app.route('/'),不运行第二个@app.route("/blog/" )
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template("index.html")
@app.route("/blog/" )
def blog_detail(blog_id):
return render_template("blog_detail.html", blog_id=blog_id, username="abc")
if __name__ == '__main__':
app.run()
这个代码应该是运行正常的,不会只运行第一个 @app.route('/')。
当你在浏览器中访问根路径 / 时,将会调用 hello_world 函数并返回 index.html 模板的内容。同样的,当你在浏览器中访问 /blog/ 时,将会调用 blog_detail 函数并返回 blog_detail.html 模板的内容,其中 blog_id 参数将被传递给模板。
答案来自 我点评开发社区 https://www.wodianping.com/
你可以尝试在浏览器中输入 http://127.0.0.1:5000/blog/123 来访问 /blog/,这样就可以测试 blog_detail 函数是否正常工作了。如果还有其他问题,请提供更多信息,以便我更好地帮助你。