Difference between using django filter filter

I have a list myList, for example. 5 elements, but I want to cut it off in a template using the command:

{% for item in myList|slice:"3" %}

or this command:

{% for item in myList|slice:":3" %}

What is the difference between slice:"x"and slice:":x"? (I don't have access to the machine with django installed, but I'm curious)

+4
source share
2 answers

slice:"3"and slice:":x"match, as they return the first 3 items from the list

but if you use slice:"2:x", then it will leave 2 elements from the first list and take from the 3rd element to the number specified in the variable x, basically taking part

+4
source

:

>>> from django.template import Template, Context
>>> Template('{{xs|slice:"3"}} {{xs|slice:":3"}}').render(Context({
...    'xs': list(range(10))
... }))
u'[0, 1, 2] [0, 1, 2]'

Django slice slice ( ) python.

class slice(stop)   #  slice|"3" -> slice(3) -> slice(None, 3, None)
class slice(start, stop[, step])  # slice|":3" -> slice(None, 3, None)
+3

Source: https://habr.com/ru/post/1680462/


All Articles