Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amending URL for "list_display_links" in Django 1.6 admin change list

What I'd like to know is how do I change the URL that's applied to the items listed in list_display_links of an admin.ModelAdmin class?

More specifically I would like /admin/contacts/contacts/12345/ to become /contacts/12345/.

All solutions I could find were quite old, somewhat convoluted and geared towards doing something else on top - so I was hoping there's some obvious way I'm missing.

(I was kind of expecting list_display_link_url (or similar) to exist to over-ride in the ModelAdmin...)

like image 382
Jon Clements Avatar asked Mar 02 '14 13:03

Jon Clements


People also ask

How do I change the admin heading in Django?

To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.

How do I find my Django admin URL?

Open a browser on the Django admin site http://127.0.0.1:8000/admin/.

How can I get previous URL in Django?

You can do that by using request. META['HTTP_REFERER'] , but it will exist if only your tab previous page was from your website, else there will be no HTTP_REFERER in META dict . So be careful and make sure that you are using . get() notation instead.


1 Answers

  1. Override standard ChangeList (in your admin.py):

    from django.contrib.admin.views.main import ChangeList
    
    class ContactChangeList(ChangeList):  
        def url_for_result(self, result):  
            pk = getattr(result, self.pk_attname)  
            # YOU PROBABLY WANT TO CHANGE NEXT LINES!
            return reverse('admin:%s_%s_change' % (self.opts.app_label, self.opts.model_name),
                           args=(quote(pk),),  
                           current_app=self.model_admin.admin_site.name)
    
  2. Tell Django admin to use your new ContactsChangeList instead of standard one:

    class ContactAdmin(admin.ModelAdmin):  
        ...  
        def get_changelist(self, request, **kwargs):  
            return ContactChangeList
    
like image 66
Alex Yakovlev Avatar answered Oct 21 '22 22:10

Alex Yakovlev