From b06eeb75dd0b2f74056e79c19bfea5c998b0af11 Mon Sep 17 00:00:00 2001 From: Foucauld Degeorges Date: Fri, 20 Dec 2024 09:33:28 +0100 Subject: [PATCH] ticket 36028: add AdminSite.template_engine option --- django/contrib/admin/actions.py | 1 + django/contrib/admin/options.py | 8 ++++++++ django/contrib/admin/sites.py | 4 +++- docs/ref/contrib/admin/index.txt | 5 +++++ tests/admin_views/sites.py | 4 ++++ tests/admin_views/test_adminsite.py | 18 ++++++++++++++++++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/admin_views/sites.py diff --git a/django/contrib/admin/actions.py b/django/contrib/admin/actions.py index 865c16aff2..3a31f236d5 100644 --- a/django/contrib/admin/actions.py +++ b/django/contrib/admin/actions.py @@ -91,4 +91,5 @@ def delete_selected(modeladmin, request, queryset): "admin/delete_selected_confirmation.html", ], context, + using=modeladmin.admin_site.template_engine ) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 69b0cc0373..9d49fe9d13 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -1403,6 +1403,7 @@ class ModelAdmin(BaseModelAdmin): "admin/change_form.html", ], context, + using=self.admin_site.template_engine, ) def _get_preserved_qsl(self, request, preserved_filters): @@ -1458,6 +1459,7 @@ class ModelAdmin(BaseModelAdmin): { "popup_response_data": popup_response_data, }, + using=self.admin_site.template_engine, ) elif "_continue" in request.POST or ( @@ -1540,6 +1542,7 @@ class ModelAdmin(BaseModelAdmin): { "popup_response_data": popup_response_data, }, + using=self.admin_site.template_engine, ) opts = self.opts @@ -1720,6 +1723,7 @@ class ModelAdmin(BaseModelAdmin): { "popup_response_data": popup_response_data, }, + using=self.admin_site.template_engine, ) self.message_user( @@ -1766,6 +1770,7 @@ class ModelAdmin(BaseModelAdmin): "admin/delete_confirmation.html", ], context, + using=self.admin_site.template_engine, ) def get_inline_formsets(self, request, formsets, inline_instances, obj=None): @@ -2035,6 +2040,7 @@ class ModelAdmin(BaseModelAdmin): { "title": _("Database error"), }, + using=self.admin_site.template_engine, ) return HttpResponseRedirect(request.path + "?" + ERROR_FLAG + "=1") @@ -2189,6 +2195,7 @@ class ModelAdmin(BaseModelAdmin): "admin/change_list.html", ], context, + using=self.admin_site.template_engine, ) def get_deleted_objects(self, objs, request): @@ -2329,6 +2336,7 @@ class ModelAdmin(BaseModelAdmin): "admin/object_history.html", ], context, + using=self.admin_site.template_engine, ) def get_formset_kwargs(self, request, obj, inline, prefix): diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index 201f28ef37..c444ba10a6 100644 --- a/django/contrib/admin/sites.py +++ b/django/contrib/admin/sites.py @@ -53,6 +53,7 @@ class AdminSite: empty_value_display = "-" login_form = None + template_engine = None index_template = None app_index_template = None login_template = None @@ -572,7 +573,7 @@ class AdminSite: request.current_app = self.name return TemplateResponse( - request, self.index_template or "admin/index.html", context + request, self.index_template or "admin/index.html", context, using=self.template_engine ) def app_index(self, request, app_label, extra_context=None): @@ -597,6 +598,7 @@ class AdminSite: self.app_index_template or ["admin/%s/app_index.html" % app_label, "admin/app_index.html"], context, + using=self.template_engine, ) def get_log_entries(self, request): diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 1b02b7d403..9cbb9c1511 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -2949,6 +2949,11 @@ Templates can override or extend base admin templates as described in The text to put at the top of the admin index page (a string). By default, this is "Site administration". +.. attribute:: AdminSite.template_engine + + Name of a template engine, as specified in your ``TEMPLATES`` setting, that + will be used to render the admin site's views. + .. attribute:: AdminSite.index_template Path to a custom template that will be used by the admin site main index diff --git a/tests/admin_views/sites.py b/tests/admin_views/sites.py new file mode 100644 index 0000000000..38f0d0b264 --- /dev/null +++ b/tests/admin_views/sites.py @@ -0,0 +1,4 @@ +from django.contrib import admin + +class CustomAdminSiteWithCustomTemplateEngine(admin.AdminSite): + template_engine = "custom_template_engine" diff --git a/tests/admin_views/test_adminsite.py b/tests/admin_views/test_adminsite.py index 7c4841f916..45e874e9b0 100644 --- a/tests/admin_views/test_adminsite.py +++ b/tests/admin_views/test_adminsite.py @@ -6,6 +6,7 @@ from django.test.client import RequestFactory from django.urls import path, reverse from .models import Article +from .sites import CustomAdminSiteWithCustomTemplateEngine site = admin.AdminSite(name="test_adminsite") site.register(User) @@ -126,3 +127,20 @@ class SiteActionsTests(SimpleTestCase): self.assertEqual(self.site.get_action(action_name), delete_selected) self.site.disable_action(action_name) self.assertEqual(self.site.get_action(action_name), delete_selected) + +class AdminSiteCustomTemplateEngineTests(TestCase): + request_factory = RequestFactory() + + @classmethod + def setUp(cls): + cls.user = User.objects.create_superuser( + username="super", password="secret", email="super@example.com" + ) + + def test_template_engine(self): + admin_site = CustomAdminSiteWithCustomTemplateEngine(name="other") + request = RequestFactory().get("/") + request.user = self.user + index_response = admin_site.index(request=request) + + self.assertEqual(index_response.using, CustomAdminSiteWithCustomTemplateEngine.template_engine) \ No newline at end of file