How to set custom forloop starting point in django template

There is a prelude in java where I can tell where to start and where to the end:

for(int i=10;i<array.length;i++){ } 

but how can I implement this int i=10 in a django template? How can I set the start and end point on my own?

forloop.first and forloop.last , but they are defined inside the loop, and I can not do something like this ?:

 {{forloop.first=10}} {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} {{forloop.last=20}} 

I read django doc, but this function does not seem to exist

+4
source share
1 answer

How about using the built-in slice filter:

 {% for athlete in athlete_list|slice:"10:20" %} <li>{{ athlete.name }}</li> {% endfor %} 

If you need to do a number loop (like python range ), you will need a custom template tag, like this one: http://djangosnippets.org/snippets/1926/

See other range snippets:

See also:

By the way, this does not look like a task for templates - consider the possibility of transferring a range from a view. And, FYI, there was a proposal to make this tag, but it was rejected because it is trying to lead to programming in the template. - Think about it.

+5
source

Source: https://habr.com/ru/post/1500394/


All Articles