views:

38

answers:

2

I am trying to output the first 255 characters of a description on a list of items and am looking for a method to get that.

Example: I have a variable that contains 300 or so characters.

I call that variable like this, {{ my_variable|characterlimit:255 }}

and it would return only the first 255 characters of that variable.

If this tag doesn't exist, I will simply create it (and suggest that it goes into django), but I wanted to make sure it didn't before I took the time to do that. Thanks!

A: 

It doesn't exist unfortunately. There are moves to implement it, but it's still in the design stage (well, implemented, but waiting for design decision), as described here.

Patch attached to that ticket contains implementation.

tkopczuk
+2  A: 

If the "my_variable" is a string, you can take advantage of the slice filter, which treats the string as a list of characters. If it's a set of words, the rough equivilant is truncatewords - but that doesn't quite sound like your need.

Usage would be something like

{{ my_variable|slice:":255" }}
heckj