【问题标题】:How to display all images in a directory with flask [duplicate]如何使用烧瓶显示目录中的所有图像[重复]
【发布时间】:2018-06-01 08:10:32
【问题描述】:

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

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>`

并且当模板成功渲染时,不会获取模板。在新选项卡中打开图像会产生: @987654321@

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

&lt;img src="{{url_for('static', filename='{{hist}}')}}" alt="{{hist}}"&gt;

【问题讨论】:

    标签: python html flask


    【解决方案1】:

    尝试将行更改为:

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

    你有一组额外的{{ }} 在那里呈现为%7B%7D

    【讨论】:

    • 所以基本上只要你在 {{ }} 烧瓶里就知道它所知道的一切意味着什么?如果我在这里的措辞令人困惑,请告诉我
    • 是的,因为你已经在 {{ }} Flask 可以解释变量hist
    【解决方案2】:

    我通过这行获得了成功:

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

    当我之前尝试过时,我可能在这里有一个额外的“情节/”。

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 2018-11-16
      • 2021-03-01
      • 2020-09-26
      • 2022-05-18
      • 2012-07-02
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多