如何在烧瓶中显示目录中的所有图像 [英] How to display all images in a directory with flask

查看:72
本文介绍了如何在烧瓶中显示目录中的所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以静态(静态/图形)显示特定目录中的所有图像.这是我的python:

I am trying to display all images from a particular directory in static (static/plots). Here is my 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

And when the template successfully renders, the templates are not fetched. Opening the image in a new tab yields: http://127.0.0.1:8080/static/%7B%7Bhist%7D%7D

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

I imagine the problem is with this line, but I can't figure out what's correct:

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

推荐答案

尝试将行更改为此:

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

您在其中还有一组{{ }},它们分别渲染为%7B%7D.

You had an extra set of {{ }} in there which was rendering as %7B and %7D.

这篇关于如何在烧瓶中显示目录中的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆