How to permanently remove html tags from django template (filter)?
I trying to use "striptags" and "removetags:" but not quit开发者_开发问答e working. Text is something like this:
bla, bla, bla <p style="margin-top: 10px; margin-right: 0px; ....
If i use striptags or removetags with truncatewords, template completely breaks down.
you should clean the string first then use truncate words.. you can use regular expression to clean tags if strip_tags is not working properly.
import re
string = "<a href=''>abc</a>"
string = re.sub("<\!?\\\\?\/?\w+[^>]*>", "", string)
This regex will clean all tags opening, closing and comments tags. this is simple solution, you can make regex strict. instead of \w+ you can use (?:td|span|div...and so on) to specify which tags you want to clean.
Either strip tags before truncating the text, or use HTML-aware truncatewords_html
. You're operating on corrupted data if you don't.
精彩评论