Reputation: 6712
Could someone tell me, does the method like substr in PHP (http://pl2.php.net/manual/en/function.substr.php) exist in Django templates?
Upvotes: 21
Views: 21131
Reputation: 818
See truncatechars, django docs filters:
<p>Example {{ person.names|truncatechars:20 }}</p>
Upvotes: 1
Reputation: 15573
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']
.
Upvotes: 2
Reputation: 165
you can use cut
filter e.g. :
{{ value }} -> 'hello world'
{{ value|cut:'hello ' }} -> 'world'
Upvotes: 5
Reputation: 89867
In python, substrings are accessed as slices; there's a built-in slice filter in django.
Upvotes: 5
Reputation: 37177
You can use the slice
filter, though I don't think there's an equivalent to the $length
argument.
Upvotes: 30