Django-admin : How to display link to object info page instead of edit form , in records change list?

12,480

Solution 1

If I understand your question right you want to add your own link to the listing view, and you want that link to point to some info page you have created.

To do that, create a function to return the link HTML in your Admin object. Then use that function in your list. Like this:

class ModelAdmin(admin.ModelAdmin):
    def view_link(self):
        return u"<a href='view/%d/'>View</a>" % self.id
    view_link.short_description = ''
    view_link.allow_tags = True
    list_display = ('id', view_link)

Solution 2

Take a look at: http://docs.djangoproject.com/en/dev/ref/contrib/admin/, ModelAdmin.list_display part, it says: A string representing an attribute on the model. This behaves almost the same as the callable, but self in this context is the model instance. Here's a full model example:

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

class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'colored_name')

So I guess, if you add these two methods to Person

def get_absolute_url(self):
    return '/profiles/%s/' % (self.id)

def profile_link(self):
    return '<a href="%s">%s</a>' % (self.get_absolute_url(), self.username)
profile_link.allow_tags = True

and changes PersonAdmin to

class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'colored_name', 'profile_link')

Then you done

Share:
12,480
Hamza
Author by

Hamza

Updated on July 02, 2022

Comments

  • Hamza
    Hamza almost 2 years

    I am customizing Django-admin for an application am working on . so far the customization is working file , added some views . but I am wondering how to change the records link in change_list display to display an info page instead of change form ?!

    in this blog post :http://www.theotherblog.com/Articles/2009/06/02/ extending-the-django-admin-interface/ Tom said :

    " You can add images or links in the listings view by defining a function then adding my_func.allow_tags = True "

    which i didn't fully understand !!

    right now i have profile function , which i need when i click on a member in the records list i can display it ( or adding another button called - Profile - ) , also how to add link for every member ( Edit : redirect me to edit form for this member ) .

    How i can achieve that ?!

  • Hamza
    Hamza almost 15 years
    Thanks thats what i was looking for :D
  • will-hart
    will-hart over 11 years
    Thanks... had to make one minor modification: def view_link(self, obj):
  • Greg Kaleka
    Greg Kaleka over 5 years
    Note that in Django 2+, allow_tags is no longer supported. Instead wrap the returned string in mark_safe() from django.utils.safestring.