star98
star98

Reputation: 327

Django: Is there a way to get items from index 0 - 5 in a list?

I've been looking around and for some reason I can only find answers that point toward getting one item from a list. Using python to demonstrate exactly what I want to do:

list = [1,2,3,4,5,6,7,8,9,0]     #list containing 10 integers
for item in list[:5]:            #for the items between index 0 and one less than 5
    print(item)                  #print the item

Is there a way to do the list[:5] part in django template code? I can't seem to wrap my head around the documentation as it is a bit confusing to a beginner such as myself. I understand how to get a SINGLE item from one index using the template code, but not multiple items from a specific index series of a list, if that makes sense. All help is appreciated!

Upvotes: 0

Views: 182

Answers (1)

zero323
zero323

Reputation: 330163

You can use slice filter.

{{ foo|slice:":5" }}

Upvotes: 2

Related Questions