Truncate filter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
@register.filter
def trunc( string, number, dots='...'):
  """ 
  truncate the {string} to {number} characters
  print {dots} on the end if truncated

  usage: {{ "some text to be truncated"|trunc:6 }}
  results: some te...
  """
  if not isinstance(string, str): string = str(string)
  if len(string) <= number:
    return string
  return string[0:number]+dots

More like this

  1. truncate by alfor 6 years, 2 months ago
  2. Truncate string after a given number of chars keeping whole words by rix 4 years, 5 months ago
  3. Truncate HTML without breaking tags by olau 4 years, 1 month ago
  4. truncatechars filter by semente 3 years, 9 months ago
  5. truncatehtml_at_word by zakj 4 years, 7 months ago

Comments

(Forgotten your password?)