开发者

How to permanently remove html tags from django template (filter)?

开发者 https://www.devze.com 2023-03-24 01:33 出处:网络
I trying to use \"striptags\" and \"removetags:\" but not quit开发者_开发问答e working. Text is something like this:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消