如何在TextField中保存带有标签和参数的链接

时间:2010-05-27 16:04:29

标签: django django-models django-templates

我有这个简单的Post模型:

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)     

我希望当我在表单中插入链接时,这些链接会从此表单保存在正文中:

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

到这个表单(带有tag和rel =“nofollow”参数):

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

我该怎么做?

谢谢^ _ ^

1 个答案:

答案 0 :(得分:1)

我假设您要保存帖子的表单数据。您希望此帖子在稍后呈现时使用有效的HTML链接显示,而不仅仅是文本,例如stackoverflow.com

为什么不推迟这一步直到你发布帖子的那一刻?这将使您的数据库内容“无HTML”,并且在服务器端即时将链接文本转换为工作HTML链接并不是一件大事。您甚至可以将这些文本链接转换为客户端的HTML链接,例如通过javascript。

进一步阅读:Replace URL with a link using regex in python