@register.filter(is_safe=True)
@stringfilter
def truncatewords(value, arg):
 try:
  length = int(arg)
 except ValueError:
  return value
 return Truncator(value).words(length, truncate=' ...')

@ register . filter ( is_safe = True ) @ stringfilter def truncatewords ( value , arg ) : try : length = int ( arg ) except ValueError : return value return Truncator ( value ) . words ( length , truncate = ' ...' )
truncates a string after a certain number of words .