开发者

Django substr / substring in templates

Could someone tell me, does the metho开发者_Python百科d like substr in PHP (http://pl2.php.net/manual/en/function.substr.php) exist in Django templates?


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


you can use cut filter e.g. :

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


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


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'].


See truncatechars, django docs filters:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜