4

I need to generate external links in admin interface grid column, but they shows as html code:

<a href="http://www.site.com/">site</a>

Admin interface translates my links as html-entities and they doesn't shows as right links. Is it possible to show external links there, not html code?

I think list_display_links doesn't work for this purpose.

Thank you!

ramusus
  • 7,789
  • 5
  • 38
  • 45

2 Answers2

9

Just go further in http://docs.djangoproject.com/en/dev/ref/contrib/admin/#list-display

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
Andrea Di Persio
  • 3,246
  • 2
  • 24
  • 23
3

The proper way to do this in Django 2.0 is to mark the string as safe HTML. Even better is to use format_html() so that Django can still escape the arguments.

from django.utils.html import format_html

def my_link_field(self):
    return format_html(
            '<a href="{0}">{1}</a>',
            self.my_external_url,
            self.my_link_display_name,
        )