slice Filter

Data Structure Most Useful

Argument: slice (required) – the slice to return.


Returns a slice of a sequence.


company = 'Webucator'


{{ company|slice:'4:7' }}




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:

  {% 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 %}
<button data-toggle="collapse" data-target=".class-event-collapse" class="class-event-dates">See More Colors</button>

Did we get something wrong? Is there a use case for the slice filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Oct. 30, 2022, 1:21 p.m. EST