如何使用flask显示目录中的所有图像

Cho*_*ots 5 html python flask

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

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

和我的 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>`
Run Code Online (Sandbox Code Playgroud)

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

我想问题出在这条线上,但我不知道什么是正确的:

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

Mat*_*aly 5

尝试将行更改为:

<img src="{{url_for('static', filename=hist)}}" alt="{{hist}}">
Run Code Online (Sandbox Code Playgroud)

你在那里有一组额外的{{ }},它渲染为%7B%7D