Question

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]'
Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top