Jazi
Jazi

Reputation: 6712

Django substr / substring in templates

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

Answers (5)

Barrrettt
Barrrettt

Reputation: 818

See truncatechars, django docs filters:

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

Upvotes: 1

Ghasem
Ghasem

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

Farsheed Feeruzy
Farsheed Feeruzy

Reputation: 165

you can use cut filter e.g. :

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

Upvotes: 5

Wooble
Wooble

Reputation: 89867

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

Upvotes: 5

Ismail Badawi
Ismail Badawi

Reputation: 37177

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

Upvotes: 30

Related Questions