Using index from iterated list

后端 未结 2 1225
萌比男神i
萌比男神i 2020-12-07 00:28

I\'m trying to display values from a different list based on the index that is currently being iterated over on another list but cannot figure out how to access the individu

2条回答
  •  我在风中等你
    2020-12-07 01:04

    It sounds like you want to iterate over two lists at the same time, in other words zip() lists.

    If this is the case, it is better to do this in the view and pass inside the context:

    headers = ["X", "Y", "Z", "XX", "YY"]
    data = zip(headers, myarray.all())
    return render(request, 'template.html', {'data': data})
    

    Then, in the template:

    {% for header, row in data %}
        
            {{ header }}
            {{ row }}
        
    {% endfor %}
    

提交回复
热议问题