Django template tag to truncate text

DjangoDjango Templates

Django Problem Overview


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?

Django Solutions


Solution 1 - Django

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

{{ value|truncatechars:9 }}

See doc here

Solution 2 - Django

{{ value|slice:"5" }}{% if value|length > 5 %}...{% endif %}

Update

Since version 1.4, Django have a built-in template tag for this:

{{ value|truncatechars:9 }}

Solution 3 - Django

I made my own template filter, that add "..." to the end of (last word of) the (truncated) string as well:

from django import template
register = template.Library()

@register.filter("truncate_chars")
def truncate_chars(value, max_length):
    if len(value) > max_length:
        truncd_val = value[:max_length]
        if not len(value) == max_length+1 and value[max_length+1] != " ":
            truncd_val = truncd_val[:truncd_val.rfind(" ")]
        return  truncd_val + "..."
    return value

Solution 4 - Django

If you prefer to create your own custom template tag, consider to use the Django util Truncator in it. The following is a sample usage:

>>> from django.utils.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").words(1)
u'Django...'
Truncator("Django template tag to truncate text").chars(20)
u'Django template t...'
Truncator("Django template tag to truncate text").chars(10)
u'Django ...'

Then you can put it in a template tag:

from django import template
from django.utils.text import Truncator

register = template.Library()

@register.filter("custom_truncator")
def custom_truncator(value, max_len, trunc_chars=True):
    truncator = Truncator(value)
    return truncator.chars(max_len) if trunc_chars else truncator.words(max_len)

Solution 5 - Django

Here it is in the Django Documentation, Built-in template tags and filters: truncatechars

Solution 6 - Django

You should write a custom template filter: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters

Have a look at how truncatewords is built in django.utils.text

Solution 7 - Django

You can achieve your goal with similar code:

{{ value_of_text|truncatechars:NUM_OF_CHARS_TO_TRUNCATE}}

where NUM_OF_CHARS_TO_TRUNCATE is number of chars to leave.

Solution 8 - Django

Solution 9 - Django

Adding a "truncate" filter was a feature request for 4 years but finally landed in trunk, as far as I understand https://code.djangoproject.com/ticket/5025 - so we’ve to wait for the next release or use trunk.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongrigyView Question on Stackoverflow
Solution 1 - DjangoBanjerView Answer on Stackoverflow
Solution 2 - DjangocaioView Answer on Stackoverflow
Solution 3 - DjangojkiView Answer on Stackoverflow
Solution 4 - DjangoDosView Answer on Stackoverflow
Solution 5 - DjangopmourelleView Answer on Stackoverflow
Solution 6 - DjangoAbid AView Answer on Stackoverflow
Solution 7 - DjangoAbuko SidneyView Answer on Stackoverflow
Solution 8 - DjangoIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 9 - DjangoHrabanView Answer on Stackoverflow