A
A
aLap2021-11-18 12:38:11
Django
aLap, 2021-11-18 12:38:11

How to get value from list by index using forloop.counter in Django template?

Greetings!

The task is as follows:
There are two lists that are passed to the template
1: 2: It is necessary to iterate over the data list in the template and associate it with the values ​​from the colors list, I try like this:
data = ['foo', 'bar', ...]
colors = ['#54BE50', '#76C008', ...]


{% for data_item in data %}
    <mark id={{ forloop.counter }} style="background: {{ colors.forloop.counter0 }};">{{ data_item }}</mark>
{% endfor %}


That is, I need to select a value from another list by a similar index in a loop through one list, maybe there is an opportunity to do this in a template? forloop.counter doesn't work when used as an index on another list.
Thank you!

UPD. The task is solved, I processed the data in the view, but, nevertheless, the solutions directly in the template are interesting.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Shatalov, 2021-11-19
@netpastor

https://stackoverflow.com/questions/4651172/refer...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question