How to limits the number of characters/words in Django templates

created at 07-15-2021 views: 20

Django uses templates filter to limit the number of characters

  • If the desired stage is a string, you can use a slice filter, which treats the string as a list of characters. 
  • If it is a set of words, the rough equivalent words are truncatewords.

truncatewords added an ellipsis at the end of the truncated result.

Example of slice usage

{{ variable|slice:":255" }}

Examples of using truncatewords

{{ variable|truncatechars:255 }}
created at:07-15-2021
edited at: 07-15-2021: