0

I am going to print four array values simultaneously in django template . like this the four arrays are of equal length ,they are header_list , domain_list , domain_data , dom

for i in range(10):
      print headerlist[i]
      print domain_list[i]
      print domain_data[i]
      print dom[i]

how to acheive these in django template .

also tried

from django import template

register = template.Library()
@register.filter(name='myrange')
def myrange(number):
    return range(number)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Syed Jafer
  • 310
  • 3
  • 13
  • Possible duplicate of [How to access array elements in a Django template?](https://stackoverflow.com/questions/1700661/how-to-access-array-elements-in-a-django-template) – Satendra Jan 16 '18 at 05:44

2 Answers2

1

You should zip these items in the view, rather than sending them separately to the template.

data = zip(header_list , domain_list , domain_data , dom)
return render(request, 'my_template.html', {'data': data, ...})

Now you can do:

{% for item in data %}
    Header: {{ item.0 }}
    Domain: {{ item.1 }}
    Domain data: {{ item.2 }}
    Dom: {{ item.3 }}
{% endfor %}
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

It can be done this way:

views.py

n = len(headerlist)
context['n'] = range(n)

Now in template

{% for i in n %}
   {{headerlist.i}}
   {{domain_list.i}}
   {{domain_data.i}}
   {{dom.i}}
{% endfor %}
Astik Anand
  • 12,757
  • 9
  • 41
  • 51