Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I truncate text and remove html tags in django template

I've used truncatechars but if text contains a html tag, it will show the tag for me.

{{content.text|safe|truncatechars:140}}

for example displays something like this:

<p>hi</p>

I want to remove p tag.

like image 858
Ali Soltani Avatar asked Dec 23 '22 10:12

Ali Soltani


1 Answers

Updated along with question:

To completely remove the tags before truncating, use:

{{ content.text|striptags|truncatechars:140 }}

Original answer:

To ensure the tags are broken correctly, you'll want to use the truncatechars_html template filter.

like image 72
meshy Avatar answered Apr 29 '23 18:04

meshy