[Django]-Django substr / substring in templates

30👍

You can use the slice filter, though I don’t think there’s an equivalent to the $length argument.

6👍

you can use cut filter e.g. :

{{ value }} -> 'hello world'
{{ value|cut:'hello ' }} -> 'world'

5👍

In python, substrings are accessed as slices; there’s a built-in slice filter in django.

👤Wooble

2👍

As everyone felt leaving a link would be enough, I’ll add some code sample from django documents here:

slice filter returns a slice of the list

{{ some_list|slice:":2" }}

If some_list is ['a', 'b', 'c'], the output will be ['a', 'b'].

👤Ghasem

1👍

See truncatechars, django docs filters:

<p>Example {{ person.names|truncatechars:20 }}</p>

Leave a comment