How to crop html without breaking tags?

How to ensure that all html tags are closed? The problem arises because I want to create some kind of excerpt for each article. For example, someone writes an article as follows:

Hi everyone, I'm just an article and I have few <strong>tags</strong> inside <em>of me</me>

If I cut this message immediately after the "tags", I get an unclosed tag. How can I check with Django all user input text before saving it to DB?

+4
source share
2 answers

In Django 1.7, there is a special template filter called truncatechars_html:

truncatechars, , HTML. , .

. truncatewords_html.

Django < 1.7 truncatewords_html , , 1.7 (), snippet ( ). . snippet: HTML.

+3

Source: https://habr.com/ru/post/1543808/


All Articles