[Django]-Shortening HTML content python / django

5๐Ÿ‘

โœ…

If you need to do it in a template take a look at truncatewords-html: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#truncatewords-html

Or elsewhere you can use the underlying django.utils.text.Truncator class (https://github.com/django/django/blob/1.7/django/utils/text.py#L65) that should work for most use cases, if you need some custom behavior use that code as a reference to implement it yourself.

Update:

If you need to use django.utils.text.Truncator directly, this is how you instantiate and call the words() method (this is exactly how truncatewords-html filter does)

Truncator(value).words(length, html=True, truncate=' ...')
๐Ÿ‘คgonz

Leave a comment