How to save links with tags and parameters in TextField
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 ^_^
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: Replace URL with a link using regex in python
精彩评论