Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django迭代模板中的静态文件_Python_Django_Django Templates - Fatal编程技术网

Python Django迭代模板中的静态文件

Python Django迭代模板中的静态文件,python,django,django-templates,Python,Django,Django Templates,我是Django的新手,我正在努力解决这个问题:在我的模板中,我想在视图中定义的数组上迭代,在每个值的末尾添加“.png”,这样我就可以将它们用作src值来表示 {%endwith%} {%endfor%} 当我打印myImg时,它的值仅为“.png”,没有iter值 也许我不能在循环中使用带有标记的?如果是这样,我如何连接我的路径、文件名和扩展名 提前感谢你把事情搞得太复杂了 static标记所做的就是将settings.static\u URL的值与文件名连接起来。您可以自己更轻松地做到这

我是Django的新手,我正在努力解决这个问题:在我的模板中,我想在视图中定义的数组上迭代,在每个值的末尾添加“.png”,这样我就可以将它们用作
src
值来表示

{%endwith%}
{%endfor%}
当我打印
myImg
时,它的值仅为“.png”,没有
iter

也许我不能在循环中使用带有标记的
?如果是这样,我如何连接我的路径、文件名和扩展名


提前感谢

你把事情搞得太复杂了

static
标记所做的就是将
settings.static\u URL
的值与文件名连接起来。您可以自己更轻松地做到这一点:

<table>
    <tr>
      {% for iter in array %}
          {% with 'path/to/images/'|add:iter|add:'.png' as myImg %}
              <td><img src="{% static myImg %}" alt=""></td>
          {% endwith %}
      {% endfor %}
    </tr>
  </table>
{%get\u static\u前缀为static\u URL%}
{阵列%%中的iter为%1}
{%endfor%}

事实上,这要简单得多,我还不知道标签是如何工作的,我需要阅读更多的文档。非常感谢!
  {% get_static_prefix as STATIC_URL %}

  {% for iter in array %}
        <td><img src="{{ STATIC_URL }}{{ iter }}.png" alt=""></td>
  {% endfor %}