A
A
Alexander2016-04-20 09:27:56
Django
Alexander, 2016-04-20 09:27:56

How to display for loop keys in template?

views.py:
#I get data from the database and pass it to the template
sliders = Slider.objects.filter(status=1)
my_template.html:

{% for k, v in sliders %}
            {{ k }}: {{ v }}
{% endfor %}

Either I don't read the doc well, or it's a small minus in Django?
In principle, I found a solution on the net, it consists in passing views.py to the template num_slide = range(len(sliders)) , or a self-written filter, but this is some kind of caste.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
marazmiki, 2016-04-20
@kentuck1213

In the documentation about {% for %} it is:

{% for v in sliders %}
  {{ forloop.counter0 }}: {{ v }} 
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question