【问题标题】:Django Template curly brackets inside curly brackets大括号内的 Django 模板大括号
【发布时间】:2021-04-27 23:17:58
【问题描述】:
{% if hosts %}
    <div class="row">
        {% for host in hosts %}
            {% if host.type_of == "Backend" %}
                <div class="col-sm-3">{{ host.type_of }}</div>
               {{ headers.{{ forloop.counter0 }} }}
            {% endif %}
        {% endfor %}
    </div>
{% endif %}

我将视图中的数组、主机和标题发送到我的模板,我需要获取具有当前索引的标题元素(获取标题[i])

错误提示

Could not parse the remainder: '{{ forloop.counter0' from 'headers.{{ forloop.counter0'

似乎找不到任何与此相关的示例。我怎样才能达到我想要的?

【问题讨论】:

标签: python django


【解决方案1】:

是否存在相同数量的主机和标头?如果是这样,在您看来,您可以使用 zip() 将它们压缩在一起,如下所示:

headers_and_hosts = zip(headers, hosts)

那么这将允许你在你的模板中做这样的事情:

{% for host, header in headers_and_hosts %}
   {% if host.type_of == "Backend" %}
      <div class="col-sm-3">{{ host.type_of }}</div>
      {{ header }}
   {% endif %}
{% endfor %}

【讨论】:

猜你喜欢
  • 2021-08-21
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
相关资源
最近更新 更多