Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring in a django template? [duplicate]

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] }}
like image 940
Abhishek Avatar asked Mar 11 '15 05:03

Abhishek


People also ask

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What does {% include %} do?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.

What is Forloop counter in Django?

Django for loop counter All the variables related to the counter are listed below. forloop. counter: By using this, the iteration of the loop starts from index 1. forloop. counter0: By using this, the iteration of the loop starts from index 0.

Do Django templates have multiple extends?

Use Django extends in multiple templates html to multiple HTML documents using the same document structure and DTL block content tags. This means the header can render in multiple templates.


1 Answers

Use the slice template filter:

{{ obj.name|slice:"0:3" }}
like image 101
catavaran Avatar answered Sep 30 '22 11:09

catavaran