prateek
prateek

Reputation: 119

Django admin : Storing links in database columns

In my django admin I have a database column with different urls in each row. 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.

Upvotes: 2

Views: 946

Answers (2)

lprsd
lprsd

Reputation: 87077

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.

Upvotes: 1

Kyle Wild
Kyle Wild

Reputation: 8915

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?

Upvotes: 0

Related Questions