10π
β
In Django 1.7, there is a specific template filter called truncatechars_html
:
Similar to truncatechars, except that it is aware of HTML tags. Any
tags that are opened in the string and not closed before the
truncation point are closed immediately after the truncation.
Also see truncatewords_html
.
For Django<1.7, you can either use truncatewords_html
, or make a custom filter based on the one implemented in 1.7 (source), or use this snippet (have not tested it). Also see relevant to the snippet blog post: Safe truncation of HTML.
π€alecxe
4π
For Django 1.6 there is truncatewords_html available no need to create a custom filter.
https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#truncatewords-html
π€petkostas
- Blog excerpt in Django
- Pytest and Django settings runtime changes
- How to iterate over a list in django templates?
- Api key and Django Rest Framework Auth Token
Source:stackexchange.com