在<a> tags with Python or Django

时间:2015-10-03 17:07:35

标签: python django

Using Python, how can I take a string with an unformatted link like this:

str = "<p>Here's some safe html with a link to somewhere: \
https://google.ca but there might also be some images <img \
src='https://commons.wikimedia.org/wiki/File:Man-in-circle_icon.svg' /> \
and other linky looking things</p>"

And wrap it in tags like this:

str = "<p>Here's some safe html with a link to somewhere: \
<a href='https://google.ca' target='_blank'>https://google.ca</a> but there might also be some images <img \
src='https://commons.wikimedia.org/wiki/File:Man-in-circle_icon.svg' /> \
and other linky looking things</p>"

1 个答案:

答案 0 :(得分:2)

Django正是为此准备了urlize模板过滤器。