Skip to content

Commit

Permalink
Fixed #35505 -- Added extrabody block to admin/base.html.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanivic authored and sarahboyce committed Jun 18, 2024
1 parent a0c44d4 commit ce1ad98
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions django/contrib/admin/templates/admin/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
26 changes: 26 additions & 0 deletions docs/ref/contrib/admin/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====================

Expand Down
4 changes: 3 additions & 1 deletion docs/releases/5.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 2 additions & 0 deletions tests/admin_views/templates/admin/base_site.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{% extends "admin/base.html" %}

{% block bodyclass %}bodyclass_consistency_check{% endblock %}

{% block extrabody %}extrabody_check{% endblock extrabody %}
4 changes: 4 additions & 0 deletions tests/admin_views/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,10 @@ def test_extended_bodyclass_template_change_form(self):
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(
Expand Down

0 comments on commit ce1ad98

Please sign in to comment.