Vic
Vic

Reputation: 177

What is the Django filter for truncating characters?

On the Django docs for built in tags and filters they give a filter to truncate words but not characters (letters/numbers/spaces, etc). Is there such a thing available?

Upvotes: 6

Views: 6725

Answers (4)

Vortico
Vortico

Reputation: 2749

As of Django 1.4, truncatechars is now a built-in filter in the release.

https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#truncatechars

Upvotes: 7

Hraban
Hraban

Reputation: 555

It 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.

Upvotes: 0

Kyle
Kyle

Reputation: 887

You can use the slice notation: {{ a_string_variable|slice:":5" }} This would give you the first 5 characters in the string.

Upvotes: 8

MBarsi
MBarsi

Reputation: 2457

I used this before and works fine :

http://djangosnippets.org/snippets/194/

Upvotes: 0

Related Questions