Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscripting Strings in django templates

Is it possible to subscript strings in django templates ?

What i mean is doing

print "hello"[:3]
=> hel

in django templates.

Writing {{ stringVar[:10] }} throws

TemplateSyntaxError: Could not parse the remainder: '[:10]'  from 'stringVar[:10]'
like image 782
Gautam Avatar asked Mar 03 '26 18:03

Gautam


1 Answers

You can, but the syntax is slightly different as what you're used to. Use slice:

{{ stringVar|slice:":2" }}

This will give you the first 2 elements from the list (or the first two chars from a string).

like image 66
jro Avatar answered Mar 05 '26 07:03

jro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!