Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slicing a list in Django template

If I want to get first 5 elements from a list I would do mylist|slice:"5"

but I want a range, let say from 3 to 7. something like mylist[3:8] how would I do that in template

like image 405
pankaj udaas Avatar asked May 02 '14 06:05

pankaj udaas


People also ask

How do I slice a list in Django template?

The slice filter returns the specified items from a list. You can define the from (inlcuded) and to (not included) indexes. If you use negative indexes, it means slicing from the end of the list, -1 refers to the last item, -2 refers to the second last item etc.

How to filter in django template?

Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own need.

How do you slice in Python?

Python slice() FunctionThe slice() function returns a slice object. A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item.

What are templates in Django?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.


1 Answers

you can just use

{{ mylist|slice:"3:8" }} 
like image 73
sundar nataraj Avatar answered Sep 20 '22 14:09

sundar nataraj