Python 无法从烧瓶中的 send_from_directory() 检索文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17681762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to retrieve files from send_from_directory() in flask
提问by Rijul Jain
I have a html file which references static object like this
我有一个 html 文件,它引用了这样的静态对象
<img src="img/snacks.png">
<link href="css/bluestrap.css" rel="stylesheet">
Hence the browser tries to call this via and flask fails to do so
因此浏览器试图通过调用它,而flask却没有这样做
http://127.0.0.1:5000/img/snacks.png
There are lots of such references across multiple files hence changing the references is not possible. How do i serve these static files from FLASK
多个文件中有很多这样的引用,因此无法更改引用。我如何从 FLASK 提供这些静态文件
I have copied all these static files to the 'static' folder and tried this
我已将所有这些静态文件复制到“静态”文件夹并尝试了此操作
@app.route('/<path:filename>')
def send_file(filename):
return send_from_directory('/static', filename)
However this does not work, Is there any other way to do this ? or what am i doing wrong ?
但是这不起作用,有没有其他方法可以做到这一点?或者我做错了什么?
回答by codegeek
In production, you don't want to serve static files using the flask server. I suggest you use a proper web server to do that.
在生产中,您不想使用 Flask 服务器提供静态文件。我建议您使用合适的 Web 服务器来执行此操作。
For dev, since you don't want to use url_for
, you can try to initialize your flask app as below. This way, flask knows where your static files are.
对于开发人员,由于您不想使用url_for
,您可以尝试如下初始化您的烧瓶应用程序。这样,flask 就知道您的静态文件在哪里。
app = Flask(__name__, static_folder='static')
@app.route('/<path:filename>')
def send_file(filename):
return send_from_directory(app.static_folder, filename)
See this post with a lot of info Static files in Flask - robot.txt, sitemap.xml (mod_wsgi)
看到这篇文章有很多信息 Flask 中的静态文件 - robots.txt, sitemap.xml (mod_wsgi)
回答by Sean Vieira
If you look at the docs for send_from_directory
you'll see that it takes the path to the directory in which the files are held on disk. Unless you have your image files saved in a root-level directory named static
, you'll want to update your file path:
如果您查看文档,send_from_directory
您会发现它采用了文件所在目录的路径disk。除非您将图像文件保存在名为 的根级目录中static
,否则您需要更新文件路径:
send_from_directory("/some/path/to/static", "my_image_file.jpg")
That being said, if you are using this for anything that will be under any load, it is better to ensure that your web serverserves the files, rather than serving static files from your application.
话虽如此,如果您将其用于任何负载下的任何内容,最好确保您的Web 服务器提供文件,而不是从您的应用程序提供静态文件。
回答by dAnjou
Don't use Flask's built-in server in production. It is for development only! And don't use Flask to serve static assets. It's slow! In production use a webserver in front of Flask like apache2, nginx or lighttpd. These servers are able to rewrite a URL and serve static assets.
不要在生产中使用 Flask 的内置服务器。仅供开发使用!并且不要使用 Flask 来提供静态资产。很慢!在生产中,在 Flask 前面使用网络服务器,如 apache2、nginx 或 lighttpd。这些服务器能够重写 URL 并提供静态资产。
How to deploy Flask: http://flask.pocoo.org/docs/deploying/
如何部署 Flask:http: //flask.pocoo.org/docs/deploying/
回答by Markus Unterwaditzer
I think a better way to do this would be:
我认为更好的方法是:
import flask
# ...
@app.route('/img/<fname>')
def legacy_images(fname):
return flask.redirect(flask.url_for('static', filename='img/' + fname), code=301)
Instead of sending the files on two different locations, this would do a permanent redirect to the proper URL. As others have said, it's also a good idea to serve static files directly with nginx or Apache.
这将永久重定向到正确的 URL,而不是将文件发送到两个不同的位置。正如其他人所说,直接使用 nginx 或 Apache 提供静态文件也是一个好主意。