【发布时间】:2020-12-11 05:13:54
【问题描述】:
我正在尝试遍历静态/文件夹中的图像。我可以遍历主“静态”文件夹中的图像,但是当我将它们放在“静态/文件夹”中时,我不确定如何在 html 中执行此操作。
我的 html 行(当 img 在主“静态”文件夹中时有效)
{% for file in files %}
<img src=" {% static file %}" height="800">
<p>File name:{{ file }}</p>
{% endfor %}
我的意见.py
def album1(request):
images = '/home/michal/PycharmProjects/Family/gallery/static/'
files = os.listdir(os.path.join(images))
context = {'files': files}
return render(request, 'gallery/album1/main.html', context)
如果我将视图更改为:
def album1(request):
images = '/home/michal/PycharmProjects/Family/gallery/static/'
files = os.listdir(os.path.join(images, 'folder'))
context = {'files': files}
return render(request, 'gallery/album1/main.html', context)
它按预期循环'static/folder/'中的文件名,但是我不知道如何在html中更改它,因为它将文件名添加到:/static/{{ file}}而不是/static/folder /{{ 文件 }}。 我认为我在这个特定页面上缺少某些东西或需要改变负载静态的东西?
{% load static %} # either here
<img src=" {% static file %}"> # or here?
【问题讨论】: