truncatechars_html Filter


Argument: n (required) – number of characters to truncate to.


Truncates a value to n characters and appends an ellipsis (…). It does not count HTML tags as characters and closes any tags that were left open as a result of the truncation.


blurb = '<p>You are <em>pretty</em> smart!</p>'


{{ blurb|truncatechars_html:15 }}


<p>You are <em>pretty…</em></p>


Be aware that autoescaping is on by default in Django. If you do not turn it off, either with the {% autoescape off %} tag or the safe filter, the HTML will be escaped.

The easiest way to avoid this is to chain on the safe filter:

{{ blurb|truncatechars_html:15|safe }}


If you do not want to break off in the middle of a word, use truncatewords_html instead.

Did we get something wrong? Is there a use case for the truncatechars_html filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Oct. 30, 2022, 1:21 p.m. EST