True links in the admin list

1
2
3
4
5
6
def link(field, desc):
    def _link(self):
        return '<a href="%(url)s">%(url)s</a>' % {'url': getattr(self, field)}
    _link.short_description = desc
    _link.allow_tags = True
    return _link

More like this

  1. general-purpose django XMLRC dispatcher by teepark 4 years, 3 months ago
  2. DRY custom ModelAdmin.list_display methods with a decorator by exogen 4 years, 8 months ago
  3. ParentModel and ChildManager for Model Inheritance by jpwatts 4 years, 8 months ago
  4. Multiple querysets by t_rybik 3 years, 2 months ago
  5. Online boolean switch in the admin list by sasha 5 years, 8 months ago

Comments

svetlyak (on August 31, 2007):

I have modified this code slightly, and add maxlength argument, to make admin layout happy with very long urls:

def link_field(field, desc, maxlength = 40):
    def _link(self):
        url = getattr(self, field)
        if len(url) > maxlength:
            text = url[:maxlength/2-2] + '...' + url[-(maxlength/2-1):]
        else:
            text = url
        return '<a href="%(url)s">%(text)s</a>' % {'url': url, 'text': text}
    _link.short_description = desc
    _link.allow_tags = True
    return _link

#

(Forgotten your password?)