图像显示不正确

时间:2019-02-19 08:05:36

标签: flask

你好,我正在学习烧瓶,我想插入图像。但是图像无法正确显示:导航器中只有一个图标。

我尝试将图像扩展名从.png更改为.jpg,但是它没有任何改变。我还尝试过更改导航器,但仍然保持不变。

python代码:

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/<page_name>/')
def render_static(page_name):
    return render_template(f'{page_name}.html')

if __name__ == "__main__":
   app.run()

html代码:

<!DOCTYPE html>
<html>
 <head>
     <title>Index</title>
 </head>
 <body>
    <img src="{{url_for('static', filename = 'image/Android_robot.png')}}" alt = "example" />
 </body>
</html>

1 个答案:

答案 0 :(得分:0)

图像应位于static文件夹中。这是一个如何在Flask模板中使用静态图像的示例。

文件夹结构:

├── app.py
├── static
│   └── image
│       └── bitcoin.jpg
└── templates
   └── image_example.html

app.py

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/demo/<page_name>/')
def render_static(page_name):
    return render_template('{page_name}.html'.format(page_name=page_name))

if __name__ == "__main__":
   app.run(debug=True)

模板image_example.html

<!DOCTYPE html>
<html>
 <head>
     <title>Index</title>
 </head>
 <body>
    <h2>Bitcoin image should be displayed underneath</h2>
    <img height="500px" src="{{url_for('static', filename='image/bitcoin.jpg')}}" alt = "example" />
 </body>
</html>

路线http://127.0.0.1:5000/demo/image_example/的输出:

static image in Flask example

相关问题