django slice string in template -


index.html

<td>{% if place or other_place or place_description%}{{ place}} {{ other_place}} {{place_description}}</td> 

this displaying data in template.i want truncate string if more length 80.

conditions are, 1.if place variable have more 80 character,it should truncate , need not show other 2 variable other_place , place_description.

2.if place variable , other_place variable making more 80 character,in case should truncate place_variable don't need show place_description variable.

3.if 3 , 80th character made place_description,need truncate their.

all fields not mandatory,so whatever field comes display,it should show 80 character.

need this.

thanks

you use slice pre-django 1.4:

{% if place or other_place or place_description%}     {% place|add:other_place|add:place_description pl %}         {% if pl|length > 80 %}             {{pl|slice:80}}...         {% else  %}             {{pl }}         {% endif %}     {% endwith %} {% endif %} 

if using django 1.4 or greater,

you can use truncatechars

{% if place or other_place or place_description%}     {% place|add:other_place|add:place_description pl %}        {{pl|truncatechars:80}}     {% endwith %} {% endif %} 

Comments