---
title: '|truncatewords_html'
description: >-
  truncates a value to `n` words and appends an ellipsis (…). Smart about HTML tags.
date: '2026-08-02'
categories:
  - Filter
  - String
canonical: https://coolify.djangotemplatetagsandfilters.com/filters/truncatewords_html/
doc_link: https://docs.djangoproject.com/en/6.0/ref/templates/builtins/#truncatewords-html
---

# |truncatewords_html

truncates a value to `n` words and appends an ellipsis (…). Smart about HTML tags.

## Documentation

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

### Variable

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

### Template

```django
{{ blurb|truncatewords_html:3 }}
```

### Result

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

### Autoescaping

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

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

```django
{{ blurb|truncatewords_html:3|safe }}
```

## Commentary

This is our favorite of the [truncating filters](/search/?q=truncat).
