How to ensure that all html-tags were closed? The problem arises because I want create some sort of excerpt for every article. For example someone writes an article like this:
Hi everyone, I'm just an article and I have few <strong>tags</strong> inside <em>of me</me>
If I cut this message just after "tags", I get an unclosed tag . How can I check with Django all user's input text before saving it to DB?
Best How To :
In Django 1.7, there is a specific template filter called
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.
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.