Skip to content

Simple Character Truncate Filter And Dot Dot Dot Variant

by Paul Kenjora on June 18th, 2007

A basic filter tag for truncating long strings and optionally appending a "…". I used this to clean up long URLS at Arkayne. See list on right side of main page.

Background

Say you have:

  • http://www.someverylongurl.com/with/lots/of/characters/that/dont/fit/in/a/line

You want to display only 40 chars:

  • http://www.someverylongurl.com/lots/o…

Code

The implementation for the plain and dot dot dot filter is wrapped into one file. Both are pretty strainght forward.

truncate_filter.py


from django import template

register = template.Library()

@register.filter("truncate")
def truncate(value, size):
return value[0:size]

@register.filter("truncate_dot")
def truncate_dot(value, size):
if len(value) > size and size > 3:
return value[0:(size-3)] + '...'
else:
return value[0:size]

Integration

Include the newly created filter and use it on any string. Note that the example shows both the dot dot dot variant and the plain truncate.

index.html


{% load truncate_filter %}

Most Viewed

Not A Valid Tag Library

If you get the above error then try the following:

  • Check that your load call in the template matches your templatetag filename.
  • Make sure you put the code in a directory called "templatetags".
  • Make sure you added a blank "__init__.py" file to your "templatetags" directory.

django_error.txt


TemplateSyntaxError at /
'truncate_filter' is not a valid tag library: Could not load template library from django.templatetags.truncate_filter, No module named truncate_filter
Request Method: GET
Request URL: http://www.arkayne.com/
Exception Type: TemplateSyntaxError
Exception Value: 'truncate_filter' is not a valid tag library: Could not load template library from django.templatetags.truncate_filter, No module named truncate_filter
Exception Location: /somepath/lib/python2.4/django/template/defaulttags.py in load, line 692

From → Code Snippets, Filter

  • Tim,

    It would not be hard to add an extra parameter called font size and then using some standard ratio adjust the width based on pixles.

    I did something similar a while back when implementing a PDF generator. Its not impossible but was overkill for my needs.

    The hardest part would be figuring out how to link the font size parameter with the CSS style on the page to truly get a stand alone filter.
  • Yes there is already a filter in Django that does something similar, its called: urlizetrunc.

    This filter takes it a step further, its has a "..." variant and works on any string. I used it on titles.
  • tim
    Looks good. How difficult would it be to implement based on the pixel width of the string as opposed to the character width? In other words, I don't use mono-spaced text.
  • You may have already realized this, but this is a small variation on the urlizetrunc filter that is shipped with Django. The main difference is that the Django version adds rel="nofollow" to the links, since it was intended for comment fields and the likes. You may well want to add link authority to the targets.
  • You may want to check out the Django template filter urlizetrunc() here: http://www.djangoproject.com/documentation/templates/#urlizetrunc
  • i'll probably be using this as i've been truncating a few things in this manner in my views.

    Thanks
  • You can also use the urlizetrunc filter to truncate URLs. Note that I also implemented my own character truncate filter recently because urlizetrunc does not work with emails.

    Cheers,

    Vincent.
  • I take it back, I see that this deals with not only urls, but any string.
  • Just curious...what's the difference between this and, say, the urltrunc filter that's built into django?
  • Nathaniel Whiteinge
    FYI: the default filter urlizetrunc hyperlinks the URL and truncates with dots.

    http://www.djangoproject.com/documentation/templates/#urlizetrunc
  • Isn't something like that already present in django?
    I think the filter is called truncate, or is it a part of the urlize filter? Can't check right now, posting from my mobile...
  • James Bennett
    Maybe I'm missing something, but how does this differ from the built-in "truncate" and "urlizetrunc" filters?
blog comments powered by Disqus