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
...)
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.
Open a browser on the Django admin site http://127.0.0.1:8000/admin/.
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.
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)
Tell Django admin to use your new ContactsChangeList
instead of standard one:
class ContactAdmin(admin.ModelAdmin):
...
def get_changelist(self, request, **kwargs):
return ContactChangeList
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With