首页 > 解决方案 > 如何使用烧瓶和 HTML 显示目录中的所有图像?

问题描述

我有一个包含 jpg 图像列表的文件夹。我必须使用烧瓶和 HTML 来显示它们。我有以下代码,但我不知道问题出在哪里。

这张图片显示了我拥有的目录

这张图片显示了我得到的输出

我的 app.py :

@app.route('/detection')
def detection():
    username = 'cars2'
    basepath = f"static/{username}/Images"
    dir = os.walk(basepath)
    file_list = []

    for path, subdirs, files in dir:
        for file in files:
            temp = joinPath(path + '/', file)
            file_list.append(temp)
    return render_template('detection.html', hists=file_list)

检测.html

{% extends 'base.html' %}

{% block head%}
<title>IPOD</title>
{% endblock %}

{% block body %}
<h1 align="center">IPOD</h1>
<!--<p>All images scraped from instagram.com/{{user_name}}</p>-->
    {% for hist in hists %}
        <img src="{{url_for('static', filename=hist)}}" alt="{{hist}}">
    {% endfor %}
{% endblock %}

渲染页面的源代码

标签: pythonflask

解决方案


有一个愚蠢的错误。

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

将源更改为变量名并且它起作用了


推荐阅读