I noticed there is no filter to truncate a string to a given number of
characters as apposed to just words. It can be useful. For instance I
am trying to show contact list that includes email addresses, and given
that some people have bizarrely long emails, I'd like to limit it to
some value.
Please let me know if this feature is available, or else I'll find out
how to implement it as a filter myself.
Thanks,
Sia
Regards,
David
I'll read up on textwrap now,
Regards,
Sia
>>> wrap('siasookhteh', 5)
['siaso', 'okhte', 'h']
Sia -
>Please let me know if this feature is available, or else I'll find out
>how to implement it as a filter myself.
>
>
A filter would look simple:
def truncate(value,arg):
return value[0:arg]
However it won't work for international characters. This will:
from django.conf.settings import DEFAULT_CHARSET
def truncate(value,arg):
return value.decode(DEFAULT_CHARSET)[0:arg].encode(DEFAULT_CHARSET)