0
{% 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 am sending arrays, hosts and headers from views to my template, I need to get element of header with the current index (getting the header[i])

Error says

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

Can't seem to find any examples regarding to this. How can I achieve what I want ?

aekiratli
  • 518
  • 7
  • 18

1 Answers1

1

Are there an equal number of hosts and headers? If so, you could use zip(), in your view, to zip them together as follows:

headers_and_hosts = zip(headers, hosts)

Then this would allow you to do something like this in your template:

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