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

标签 html flask

在 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/

    相关文章:

    css - 如何为 iPad Chrome 浏览器预加载图像?

    html - 如何修复 IE 7 中未出现的 UI 元素?

    javascript - Angular JS调整大小操作

    html - Bootstrap 列不居中

    python - Twitter oauth 与flask_oauthlib,无法生成请求 token

    flask - 从flask-sqlalchemy db获取所有模型

    html - 当我想在移动设备上使用 AMPed 版本时,是否必须维护两个版本的网站?

    python - 在 Flask 服务器中禁用控制台消息

    python - 从 Flask 中的另一个目录下载文件

    python - flask_cache 和 memoize - make_cache_key 错误 : object of type 'NoneType' has no len()