1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

ticket 36028: add AdminSite.template_engine option

This commit is contained in:
Foucauld Degeorges 2024-12-20 09:33:28 +01:00
parent f05edb2b43
commit b06eeb75dd
6 changed files with 39 additions and 1 deletions

View File

@ -91,4 +91,5 @@ def delete_selected(modeladmin, request, queryset):
"admin/delete_selected_confirmation.html",
],
context,
using=modeladmin.admin_site.template_engine
)

View File

@ -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):

View File

@ -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):

View File

@ -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

View File

@ -0,0 +1,4 @@
from django.contrib import admin
class CustomAdminSiteWithCustomTemplateEngine(admin.AdminSite):
template_engine = "custom_template_engine"

View File

@ -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)