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: