Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding the Django admin change_list_results.html only for some models

I want to override the templates/admin/change_list_results.html and templates/admin/change_list.html templates for just one of my models. How do I tell the admin to differentiate this model from all the others in my app and render a different change template than the default? If I just edit change_list_results.html then all the models in the admin views will reflect my changes.

like image 528
Mihai Oprea Avatar asked May 08 '11 19:05

Mihai Oprea


2 Answers

You can, see documentation here.

The short version is that you need a custom template at admin/your-app-name/your-model-name/change_list.html in your template path. It can be in an an app or in your root template directory.

The one catch is that Django needs to find it before it finds the default "admin/change_list.html" in django.contrib.admin. If it's in an app, make sure that app is listed before django.contrib.admin in INSTALLED_APPS. I usually just put admin template overrides in my root templates folder, since that gets loaded before apps.

like image 78
Chris Amico Avatar answered Nov 12 '22 05:11

Chris Amico


IMHO wont work because change_list_results.html is called from a template_tag -> result_list.

https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/change_list.html#L91

https://github.com/django/django/blob/65cf82bd08631a7aa8d9dd007b2527476fa3304f/django/contrib/admin/templatetags/admin_list.py#L288

I want to see what happens if you override or monkey patch that template tag. But import order play a lot in this.

like image 28
Esteban Feldman Avatar answered Nov 12 '22 06:11

Esteban Feldman