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

created at 07-15-2021 views: 1

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 }}
Please log in to leave a comment.