Django has truncatewords
template tag, which cuts the text at the given word count. But there is nothing like truncatechars.
What\'s the best w
I made my own template filter, that add "..." to the end of (last word of) the (truncated) string as well:
from django import template
register = template.Library()
@register.filter("truncate_chars")
def truncate_chars(value, max_length):
if len(value) > max_length:
truncd_val = value[:max_length]
if not len(value) == max_length+1 and value[max_length+1] != " ":
truncd_val = truncd_val[:truncd_val.rfind(" ")]
return truncd_val + "..."
return value
slice
Adding a "truncate" filter 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.