Django - Convert URLs to Links, Images, Objects

I am creating a simple application like comments, and you need to convert regular URLs to links, image links to images, and yt / vimeo / etc. links to flash objects. For instance:.

http://foo.bar to <a href="http://foo.bar">http://foo.bar</a> http://foo.bar/image.gif to <img src="http://foo.bar/image.gif"/> etc. 

Of course, I can write all this on my own, but I think this is such an obvious piece of code that someone has already written (perhaps even with the text split into

) For some time I searched Google, but could not find anything complicated, just a few fragments. Is there a filter (or something like that)?

Thanks!

PS. There is urlize, but it only works for the first case.

+4
source share
1 answer

Write a custom filter to handle all the necessary cases. Check out the urlize source code to get started. You will also need the urlize function from utils.

In your filter, first check the first case and name urlize. Handle the second case and any other cases you may have.

+4
source

All Articles