djangodjango-templates

Substring in a Django template?


How do I display a substring in a Django template? The following doesn't seem to work. I am trying to display the first 3 letters and the next 3 in another line:

{{ obj.name[0:3] }}
{{ obj.name[4:8] }}

Solution

  • Use the slice template filter:

    {{ obj.name|slice:"0:3" }}