114

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?

9 Answers 9

214

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

{{ value|truncatechars:9 }}

See doc here

1
  • 3
    "truncatechars" is working on latest Django 1.9 also Commented Jul 15, 2016 at 8:18
73
{{ 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 }}
4
  • Since I'm working with pre-1.4 Django, this is exactly what I needed. Thanks! Commented Dec 6, 2012 at 16:53
  • 3
    the truncatechars filter adds an ellipse character by default.
    – cnfw
    Commented Apr 6, 2017 at 13:28
  • out of the whole knowledge I have of django I constantly go back to this page to get the trunchated functions that I don't seem to remember when I want to use it, so its only write to comment and put an upvote on the greatest STF post ever ^^ Commented Jul 23, 2021 at 10:35
  • back for one more ^^ Commented Jul 31, 2021 at 20:10
12

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
1
  • 2
    Its default now it django. Commented Nov 12, 2017 at 8:58
12

If you go on creating your own custom template tag, consider to leverage the native Django util Truncator.

The following is a sample usage of the Truncator util:

>>> 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").chars(20)
u'Django template t...'

And here how you can use it inside a Django 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_words=False):
    """ Set trunc_words=True to truncate by words instead of by chars."""
    truncator = Truncator(value)
    return truncator.words(max_len) if trunc_words else truncator.chars(max_len)

Eventually you can import the custom_truncator in any Django template.

4

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

3

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

3

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.

1

slice

8
  • Thanks, but this is not quite what I need.
    – grigy
    Commented Mar 8, 2011 at 19:04
  • How so? It can be used to slice strings. Commented Mar 8, 2011 at 19:08
  • Documentation says it's for lists. Also it does not append "..." to truncated text.
    – grigy
    Commented Mar 8, 2011 at 19:17
  • 1
    The "..." needs to be added only if it got truncated.
    – grigy
    Commented Mar 8, 2011 at 19:26
  • 2
    This answers the OP's question, and this'll work with older versions of Django. Though with newer versions truncatechars should be preferred, due to it being more explicit. Commented Jun 20, 2014 at 5:17
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.