rix
rix

Reputation: 10632

Remove last 3 letters of string in Django template [:-3]

I'm doing the following:

{% for wrapping in wrappings %}  //array of strings
<input type="radio" value="{{ wrapping[:-3] }}" etc

I want to output all the string in wrapping minus the last 3 letters but am recieving a:

TemplateSyntaxError: Could not parse the remainder: '[:-3]' from 'wrapping[:-3].

Any idea what's wrong/how to do this please? Thanks,

Upvotes: 9

Views: 14319

Answers (3)

Alexander Atanasov
Alexander Atanasov

Reputation: 496

{{ variable|slice:":-3" }} would do it.

Upvotes: 0

Jon Clements
Jon Clements

Reputation: 142156

You can just use the slice filter:

{{ wrapping|slice:":-3" }}

Upvotes: 22

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

You want the slice filter for that.

Upvotes: 2

Related Questions