views:

18

answers:

1

I have this simple Post model:

class Post(models.Model):

    title = models.CharField(_('title'), max_length=60, blank=True, null=True)
    body = models.TextField(_('body'))
    blog = models.ForeignKey(Blog, related_name="posts")
    user = models.ForeignKey(User)     

I want that when I insert in the form the links, then these links are saved in the body from this form:

http://www.example.com or www.example.com

to this form ( with tag and rel="nofollow" parameter ):

<a href="http://www.example.com" rel="nofollow">www.example.com</a>

How can I do this ?

Thanks ^_^

+1  A: 

I assume you want to save your form data of a post. You want this post, when rendered later, to be displayed with working HTML links, not just text, like e.g. stackoverflow.com.

Why not postpone this step until the moment you render the post? This would keep your database content 'HTML-free' and it's not such a big deal to turn links texts into working HTML links on-the-fly on the server side. You could even turn these text links into HTML links at the client side, e.g. via javascript.

Further reading: http://stackoverflow.com/questions/1112012/replace-url-with-a-link-using-regex-in-python

The MYYN
yesss, I want to do this with javascript but I don't know. Could you help me ? Right now I am using urlize filter.Thanks ^_^
xRobot