我正在尝试使用 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/