Returns a slice of a sequence.
Variable
company = 'Webucator'
Template
{{ company|slice:'4:7' }}
Result
cat
Argument: slice
(required)
– the slice to return.
Returns a slice of a sequence.
company = 'Webucator'
{{ company|slice:'4:7' }}
cat
Did we get something wrong? Is there a use case for the
slice filter that we should add?
Please let us know.
Commentary
This is extremely useful for outputting subsets of querysets in a Django template. For example, if you only want to show the first three results of your queryset and hide the rest under a “Show More” link, you use two loops with slicing:
<ol> {% for color in widget.colors|slice:':3' %} <li>{{ color }}</li> {% endfor %} {% for color in widget.colors|slice:'3:' %} <li class="collapse class-event-collapse">{{ color }}</li> {% endfor %} </ol> <button data-toggle="collapse" data-target=".class-event-collapse" class="class-event-dates">See More Colors</button>