如何在带有烧瓶的目录中显示所有图像[复制]

问题描述 投票:4回答:2

我试图在静态(静态/绘图)中显示特定目录中的所有图像。这是我的python:

hists = os.listdir('static/plots')
hists = ['plots/' + file for file in hists]
return render_template('report.html', hists = hists)

和我的HTML:

<!DOCTYPE=html>
<html>
<head>
    <title>
        Store Report
    </title>
</head>
<body>
    {{first_event}} to {{second_event}}
    {% for hist in hists %}
    <img src="{{url_for('static', filename='{{hist}}')}}" alt="{{hist}}">
    {% endfor %}
</body>
</html>`

当模板成功呈现时,不会获取模板。在新选项卡中打开图像会产生:http://127.0.0.1:8080/static/%7B%7Bhist%7D%7D

我想问题就是这条线,但我无法弄清楚什么是正确的:

<img src="{{url_for('static', filename='{{hist}}')}}" alt="{{hist}}">

python html flask
2个回答
3
投票

尝试将行更改为:

<img src="{{url_for('static', filename=hist)}}" alt="{{hist}}">

你有一套额外的{{ }},它们呈现为%7B%7D


1
投票

我用这条线取得了成功:

<img src="static/{{hist}}" alt="{{hist}}">

我之前尝试过时可能会有一些额外的“情节/”。

© www.soinside.com 2019 - 2024. All rights reserved.