python flask - 提供静态文件

标签 python flask

我正在尝试使用 Flask 提供静态文件。我不知道如何使用 url_for 函数。我所有生成动态内容的路由都工作正常,我导入了 url_for,但是当我有这段代码时:

@app.route('/')
def home():
    return url_for('static', filename='hi.html')

连同位于静态目录中的“hi.html”文件(其中包含一些基本的 html),当我加载页面时,我得到的实际上是这样的:

/static/hi.html

我只是错误地使用了 url_for 吗?

最佳答案

url_for 只是准确地返回该文件的 URL。听起来您想重定向 到该文件的 URL。相反,您只是将 URL 的文本作为响应发送给客户端。

from flask import url_for, redirect

@app.route('/')
def home():
    return redirect(url_for('static', filename='hi.html'))

关于python flask - 提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15883874/

相关文章:

python - Flask,并非所有参数都在字符串格式化期间转换

python - 获取数组平均值的更多 pythonian 方法

python - Heroku 找不到 gunicorn 命令

python - Numpy 中的矩阵乘法(将 2d 转换为 3d)

用于编写 P2P 应用程序的 Python 库/框架

python - "Internal Server Error"与 Hello World Python 应用程序

javascript - Flask 和 gevent 的 CORS 错误

python - 错误: 502 bad gateway in flask app on nginx and uwsgi

php - 如何使用文件 IO 最好地将 python 字典传递给 PHP

python - Django 发送请求花费太多时间