Python Flask 在哪里寻找图像文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28207761/
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
Where does flask look for image files?
提问by Aritra Biswas
I am setting up a local server using flask. All I want to do currently is display an image using the img tag in the index.html page. But I keep getting error
我正在使用烧瓶设置本地服务器。我目前想要做的就是在 index.html 页面中使用 img 标签显示图像。但我不断收到错误
GET http://localhost:5000/
ayrton_senna_movie_wallpaper_by_bashgfx-d4cm6x6.jpg 404 (NOT FOUND)
Where does flask look for files? A Little help would be great. My HTML code is
Flask 在哪里查找文件?一点帮助会很棒。我的 HTML 代码是
<html>
<head>
</head>
<body>
<h1>Hi Lionel Messi</h1>
<img src= "ayrton_senna_movie_wallpaper_by_bashgfx-d4cm6x6.jpg ">
</body>
</html>
My python code is :
我的python代码是:
@app.route('/index', methods=['GET', 'POST'])
def lionel():
return app.send_static_file('index.html')
回答by Matt Healy
Is the image file ayrton_senna_movie_wallpaper_by_bashgfx-d4cm6x6.jpg in your static
directory? If you move it to your static directory and update your HTML as such:
图像文件 ayrton_senna_movie_wallpaper_by_bashgfx-d4cm6x6.jpg 在您的static
目录中吗?如果您将其移动到您的静态目录并更新您的 HTML:
<img src="/static/ayrton_senna_movie_wallpaper_by_bashgfx-d4cm6x6.jpg">
It should work.
它应该工作。
Also, it is worth noting, there is a better way to structure this.
另外,值得注意的是,有一种更好的方法来构造它。
File structure:
文件结构:
app.py
static
|----ayrton_senna_movie_wallpaper_by_bashgfx-d4cm6x6.jpg
templates
|----index.html
app.py
应用程序
from flask import Flask, render_template, url_for
app = Flask(__name__)
@app.route('/index', methods=['GET', 'POST'])
def lionel():
return render_template('index.html')
if __name__ == '__main__':
app.run()
templates/index.html
模板/index.html
<html>
<head>
</head>
<body>
<h1>Hi Lionel Messi</h1>
<img src="{{url_for('static', filename='ayrton_senna_movie_wallpaper_by_bashgfx-d4cm6x6.jpg')}}" />
</body>
</html>
Doing it this way ensures that you are not hard-coding a URL path for your static assets.
这样做可以确保您不会为静态资产硬编码 URL 路径。
回答by Burhan Khalid
From the documentation:
从文档:
Dynamic web applications also need static files. That's usually where the CSS and JavaScript files are coming from. Ideally your web server is configured to serve them for you, but during development Flask can do that as well. Just create a folder called
static
in your package or next to your module and it will be available at/static
on the application.To generate URLs for static files, use the special
'static'
endpoint name:url_for('static', filename='style.css')
The file has to be stored on the filesystem as
static/style.css
.
动态 Web 应用程序也需要静态文件。这通常是 CSS 和 JavaScript 文件的来源。理想情况下,您的 Web 服务器被配置为为您服务,但在开发过程中 Flask 也可以这样做。只需
static
在您的包中或在您的模块旁边创建一个文件夹,它就可以在/static
应用程序中使用。要为静态文件生成 URL,请使用特殊的
'static'
端点名称:url_for('static', filename='style.css')
该文件必须作为
static/style.css
.
回答by Balaji
use absolute path where the image actually exists (e.g) '/home/artitra/pictures/filename.jpg'
使用图像实际存在的绝对路径(例如)'/home/artitra/pictures/filename.jpg'
or create static folder inside your project directory like this
或者像这样在项目目录中创建静态文件夹
| templates
- static/
- images/
- yourimagename.jpg
then do this
然后这样做
app = Flask(__name__, static_url_path='/static')
then you can access your image like this in index.html
然后你可以在index.html 中像这样访问你的图像
src ="/static/images/yourimage.jpg"
in img tag
在 img 标签中
回答by pelon
It took me a while to figure this out too. url_for
in Flask looks for endpoints that you specified in the routes.py
script.
我也花了一段时间才弄清楚这一点。url_for
在 Flask 中查找您在routes.py
脚本中指定的端点。
So if you have a decorator in your routes.py
file like @blah.route('/folder.subfolder')
then Flask will recognize the command {{ url_for('folder.subfolder') , filename = "some_image.jpg" }}
. The 'folder.subfolder'
argument sends it to a Flask endpoint it recognizes.
因此,如果您的routes.py
文件中有装饰器,@blah.route('/folder.subfolder')
那么 Flask 将识别该命令{{ url_for('folder.subfolder') , filename = "some_image.jpg" }}
。该'folder.subfolder'
参数将其发送到它识别的 Flask 端点。
However let us say that you stored your image file, some_image.jpg
, in your subfolder, BUT did not specify this subfolder as a route endpoint in your flask routes.py
, your route decorator looks like @blah.routes('/folder')
. You then have to ask for your image file this way:
{{ url_for('folder'), filename = 'subfolder/some_image.jpg' }}
但是,假设您将图像文件 , 存储some_image.jpg
在您的子文件夹中,但没有将此子文件夹指定为您的烧瓶中的路由端点routes.py
,您的路由装饰器看起来像@blah.routes('/folder')
. 然后,您必须以这种方式请求您的图像文件:
{{ url_for('folder'), filename = 'subfolder/some_image.jpg' }}
I.E. you tell Flask to go to the endpoint it knows, "folder", then direct it from there by putting the subdirectory path in the filename argument.
IE 你告诉 Flask 去它知道的端点,“文件夹”,然后通过将子目录路径放在文件名参数中来引导它。