Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template tag to truncate text

Django has truncatewords template tag, which cuts the text at the given word count. But there is nothing like truncatechars.

What's the best way to cut the text in the template at given char-length limit?

like image 997
grigy Avatar asked Mar 08 '11 17:03

grigy


People also ask

How do I truncate text in Django?

text import Truncator >>> Truncator("Django template tag to truncate text") <Truncator: <function <lambda> at 0x10ff81b18>> >>>Truncator("Django template tag to truncate text"). words(3) u'Django template tag...' Truncator("Django template tag to truncate text").

What is truncate in Django?

Truncate statement in SQL is meant to empty a table for future use. Though Django doesn't provide a builtin to truncate a table, but still similar result can be achived using delete() method. For example: >>> Category. all().

What does the built in Django template tag Lorem do?

lorem. Displays random “lorem ipsum” Latin text. This is useful for providing sample data in templates. A number (or variable) containing the number of paragraphs or words to generate (default is 1).


1 Answers

This has recently been added in Django 1.4. e.g.:

{{ value|truncatechars:9 }} 

See doc here

like image 200
Banjer Avatar answered Oct 14 '22 11:10

Banjer