XinYi Chua
XinYi Chua

Reputation: 281

Django - Shorten text content retrieved in html template

I would like to know how to shorten text content retrieved to get a preview instead of getting a block of text in the html template

From: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

To:

aaaaaaaaaaaaaaaaaaaaaaaaaaa....

The code that is used to retrieve the contents is :

   <a class="list_content" href="/qna/view" onclick="document.forms['q_title_{{ q.id }}'].submit(); return false" title="{{ q.content }}">{{ q.content }} </a><br/>

Many thanks!

Upvotes: 1

Views: 662

Answers (3)

DrTyrsa
DrTyrsa

Reputation: 31951

If you are using Django 1.4, truncatechars filter will be the best way to go:

{{ q.content|truncatechars:25 }}

Upvotes: 2

San4ez
San4ez

Reputation: 8231

truncatechars template filter for django 1.4

Upvotes: 1

Alex
Alex

Reputation: 9031

Assuming that "q.content" is a string you could use the slice command:

{{ q.content|slice:":255" }}

Upvotes: 1

Related Questions