【问题标题】:Mask file name on redirect重定向时的掩码文件名
【发布时间】:2014-03-02 11:27:51
【问题描述】:

我的应用程序中有一个静态 html 页面,存储在 static/foo/bar/index.html。我想将此页面发送给/my-url/。在 Flask 中,我的路由如下所示:

@app.route('/my-url/')
def myurl():
  return redirect(url_for('static', filename='foo/bar/index.html'))

使用此路由,我的静态 html 文件出现在:

localhost:8000/my-url/index.html

我希望最终网址位于:

localhost:8000/my-url/

如何让 Flask 屏蔽最终 url 中的 index.html 文件名?

【问题讨论】:

  • 在您进行重定向时,最终 URL 将是 localhost:8000/static/foo/bar/index.html

标签: python redirect flask


【解决方案1】:

使用flask.send_from_directory() function:直接自己提供文件,而不是重定向:

from flask import send_from_directory

@app.route('/my-url/')
def myurl():
    return send_from_directory(app.static_folder, 'foo/bar/index.html')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2013-10-21
    • 2023-03-29
    • 2015-12-21
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多