Discussion:
truncate string after x characters
patrickk
2007-08-20 09:52:54 UTC
Permalink
how do I truncate a string (in the template) after a certain amount
of characters?
I know there´s a truncate-filter, but it works with words and not
characters.

thanks,
patrick
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Stefan Matthias Aust
2007-08-20 10:29:13 UTC
Permalink
Post by patrickk
how do I truncate a string (in the template) after a certain amount
of characters?
It's not difficult to create custom filters. For example

from django.template import Library

register = Library()

@register.filter
def truncate(value, arg):
try:
return value[:int(arg)]
except ValueError:
return value
--
Stefan Matthias Aust

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Malcolm Tredinnick
2007-08-20 10:33:38 UTC
Permalink
Post by patrickk
how do I truncate a string (in the template) after a certain amount
of characters?
I know there´s a truncate-filter, but it works with words and not
characters.
There's #5025 in Trac.

This shouldn't be seen as any endorsement for that ticket, since I'm not
convinced it belongs in core and haven't really worried about making any
kind of decision. But you can try it out for your own purposes (use it
to create a filter in your app, for example).

Regards,
Malcolm
--
The early bird may get the worm, but the second mouse gets the cheese.
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
patrickk
2007-08-20 10:55:01 UTC
Permalink
thanks.

since truncatewords and truncatewords_html are predefined filters, I
guess that truncate should also be one (just my opinion).

patrick
Post by Malcolm Tredinnick
Post by patrickk
how do I truncate a string (in the template) after a certain amount
of characters?
I know there´s a truncate-filter, but it works with words and not
characters.
There's #5025 in Trac.
This shouldn't be seen as any endorsement for that ticket, since I'm not
convinced it belongs in core and haven't really worried about making any
kind of decision. But you can try it out for your own purposes (use it
to create a filter in your app, for example).
Regards,
Malcolm
--
The early bird may get the worm, but the second mouse gets the cheese.http://www.pointy-stick.com/blog/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Ceph
2007-08-20 12:57:24 UTC
Permalink
{{ post.subject|slice:":15"|escape }} does the job.
Post by patrickk
thanks.
since truncatewords and truncatewords_html are predefined filters, I
guess that truncate should also be one (just my opinion).
patrick
Post by Malcolm Tredinnick
Post by patrickk
how do I truncate a string (in the template) after a certain amount
of characters?
I know there´s a truncate-filter, but it works with words and not
characters.
There's #5025 in Trac.
This shouldn't be seen as any endorsement for that ticket, since I'm not
convinced it belongs in core and haven't really worried about making any
kind of decision. But you can try it out for your own purposes (use it
to create a filter in your app, for example).
Regards,
Malcolm
--
The early bird may get the worm, but the second mouse gets the cheese.http://www.pointy-stick.com/blog/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Continue reading on narkive:
Loading...