diff options
| author | stefan.ivic <contact@stefanivic.com> | 2024-06-08 15:44:17 +0200 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-06-18 16:49:53 +0200 |
| commit | ce1ad98565d98b7939367b2bcab755c3555ceb42 (patch) | |
| tree | 334799428166190114e9b72089661947ee9d99ac | |
| parent | a0c44d4e23f8f509757f97f28fbbb1ced3382361 (diff) | |
Fixed #35505 -- Added extrabody block to admin/base.html.
| -rw-r--r-- | django/contrib/admin/templates/admin/base.html | 1 | ||||
| -rw-r--r-- | docs/ref/contrib/admin/index.txt | 26 | ||||
| -rw-r--r-- | docs/releases/5.2.txt | 4 | ||||
| -rw-r--r-- | tests/admin_views/templates/admin/base_site.html | 2 | ||||
| -rw-r--r-- | tests/admin_views/tests.py | 4 |
5 files changed, 36 insertions, 1 deletions
diff --git a/django/contrib/admin/templates/admin/base.html b/django/contrib/admin/templates/admin/base.html index f01a7ab61c..18e3a2a9fc 100644 --- a/django/contrib/admin/templates/admin/base.html +++ b/django/contrib/admin/templates/admin/base.html @@ -121,5 +121,6 @@ <symbol viewBox="0 0 24 24" width="1rem" height="1rem" id="icon-sun"><path d="M0 0h24v24H0z" fill="currentColor"/><path d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/></symbol> </svg> <!-- END SVGs --> +{% block extrabody %}{% endblock extrabody %} </body> </html> diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 20f2ce7582..504ab62368 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -2827,6 +2827,32 @@ linked to the document in ``{% block dark-mode-vars %}``. .. _prefers-color-scheme: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme +.. _extrabody: + +``extrabody`` block +=================== + +.. versionadded:: 5.2 + +You can add custom HTML, JavaScript, or other content to appear just before the +closing ``</body>`` tag of templates that extend ``admin/base.html`` by +extending the ``extrabody`` block. For example, if you want an alert to appear +on page load you could add a ``admin/base.html`` template override to your +project: + +.. code-block:: html+django + + {% extends 'admin/base.html' %} + + {% block extrabody %} + {{ block.super }} + <script> + document.addEventListener('DOMContentLoaded', function() { + window.alert('Welcome!'); + }); + </script> + {% endblock extrabody %} + ``AdminSite`` objects ===================== diff --git a/docs/releases/5.2.txt b/docs/releases/5.2.txt index fb2a1d0177..0a0cfb0aef 100644 --- a/docs/releases/5.2.txt +++ b/docs/releases/5.2.txt @@ -37,7 +37,9 @@ Minor features :mod:`django.contrib.admin` ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -* ... +* The ``admin/base.html`` template now has a new block + :ref:`extrabody <extrabody>` for adding custom code before the closing + ``</body>`` tag. :mod:`django.contrib.admindocs` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/admin_views/templates/admin/base_site.html b/tests/admin_views/templates/admin/base_site.html index 9714571289..27158e9e44 100644 --- a/tests/admin_views/templates/admin/base_site.html +++ b/tests/admin_views/templates/admin/base_site.html @@ -1,3 +1,5 @@ {% extends "admin/base.html" %} {% block bodyclass %}bodyclass_consistency_check{% endblock %} + +{% block extrabody %}extrabody_check{% endblock extrabody %} diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index cb6815e7a8..763fa44ce8 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -1746,6 +1746,10 @@ class AdminCustomTemplateTests(AdminViewBasicTestCase): response = self.client.get(reverse("admin:admin_views_section_add")) self.assertContains(response, "bodyclass_consistency_check ") + def test_extended_extrabody(self): + response = self.client.get(reverse("admin:admin_views_section_add")) + self.assertContains(response, "extrabody_check\n</body>") + def test_change_password_template(self): user = User.objects.get(username="super") response = self.client.get( |
