Overriding the Django admin change_list_results.html only for some models

孤人 提交于 2019-12-23 10:53:17

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5929854/overriding-the-django-admin-change-list-results-html-only-for-some-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!