gpt4 book ai didi

html - Flask网页路由是否在结尾处使用正斜杠?

转载 作者:行者123 更新时间:2023-12-03 17:30:49 26 4
gpt4 key购买 nike

在 Flask 的 main.py 文件中为页面指定路由路由时,例如:

@app.route('/home')
def home():
return render_template('home.html')

当我浏览到 localhost:nnnn/about 时出现错误,因为浏览器会自动将尾部正斜杠附加到页面(即 localhost:nnnn/about/)。

有没有办法将此问题作为“仅一次”配置来处理,还是我需要在每个功能上方有两个应用程序路由标签? IE:
@app.route('/home')
@app.route('/home/')
def ....

最佳答案

flask 文档提到了这一点:Unique URLs / Redirection Behavior .

简而言之:

  • 路由有'/':有或没有'/'都可以,没有尾部斜杠将被重定向到带有尾部斜杠的规范URL。
  • 路由没有'/':只有没有斜杠的 URL 可以访问它。带有尾部斜杠会导致 404 错误。
  • 关于html - Flask网页路由是否在结尾处使用正斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34234355/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com