开发者

Django admin : Storing links in database columns

In my django admin I have a database column with different urls in each row.开发者_StackOverflow中文版 These urls are displayed as simple texts and not as link. How can I make them links so that they redirect the user to the url when they click on it.


Set allow_tags on the method on the model to true:

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    color_code = models.CharField(max_length=6)

    def colored_name(self):
        return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
    colored_name.allow_tags = True

from the django documentation.


If you have access to your templates:

If the contents of the field are something like:

http://www.google.com

You could output this in your template to make them clickable:

<a href="{{ field }}">{{ field }}</a>

If not:

I think this Stacko question is similar: How to add clickable links to a field in Django admin?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜