1
0
mirror of https://github.com/django/django.git synced 2025-04-12 03:22:21 +00:00

Fixed #36121 -- Allowed customizing the admin site password change form.

This commit is contained in:
Mohammadreza Eskandari 2025-01-21 12:34:35 +03:00 committed by Sarah Boyce
parent c6ace896a2
commit 12b9ef38b3
6 changed files with 24 additions and 3 deletions

View File

@ -57,6 +57,7 @@ class AdminSite:
app_index_template = None
login_template = None
logout_template = None
password_change_form = None
password_change_template = None
password_change_done_template = None
@ -355,7 +356,7 @@ class AdminSite:
url = reverse("admin:password_change_done", current_app=self.name)
defaults = {
"form_class": AdminPasswordChangeForm,
"form_class": self.password_change_form or AdminPasswordChangeForm,
"success_url": url,
"extra_context": {**self.each_context(request), **(extra_context or {})},
}

View File

@ -2980,6 +2980,13 @@ Templates can override or extend base admin templates as described in
Path to a custom template that will be used by the admin site logout view.
.. attribute:: AdminSite.password_change_form
.. versionadded:: 6.0
Subclass of :class:`~django.contrib.auth.forms.PasswordChangeForm` that
will be used by the admin site password change view.
.. attribute:: AdminSite.password_change_template
Path to a custom template that will be used by the admin site password

View File

@ -48,7 +48,8 @@ Minor features
:mod:`django.contrib.admindocs`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ...
* The new :attr:`.AdminSite.password_change_form` attribute allows customizing
the form used in the admin site password change view.
:mod:`django.contrib.auth`
~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -18,6 +18,7 @@ class Admin2(admin.AdminSite):
login_template = "custom_admin/login.html"
logout_template = "custom_admin/logout.html"
index_template = ["custom_admin/index.html"] # a list, to test fix for #18697
password_change_form = forms.CustomAdminPasswordChangeForm
password_change_template = "custom_admin/password_change_form.html"
password_change_done_template = "custom_admin/password_change_done.html"

View File

@ -1,4 +1,4 @@
from django.contrib.admin.forms import AdminAuthenticationForm
from django.contrib.admin.forms import AdminAuthenticationForm, AdminPasswordChangeForm
from django.contrib.admin.helpers import ActionForm
from django.core.exceptions import ValidationError
@ -14,6 +14,12 @@ class CustomAdminAuthenticationForm(AdminAuthenticationForm):
return username
class CustomAdminPasswordChangeForm(AdminPasswordChangeForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["old_password"].label = "Custom old password label"
class MediaActionForm(ActionForm):
class Media:
js = ["path/to/media.js"]

View File

@ -1820,6 +1820,11 @@ class AdminCustomTemplateTests(AdminViewBasicTestCase):
response = user_admin.user_change_password(request, str(user.pk))
self.assertContains(response, '<div class="help">')
def test_custom_password_change_form(self):
self.client.force_login(self.superuser)
response = self.client.get(reverse("admin4:password_change"))
self.assertContains(response, "Custom old password label")
def test_extended_bodyclass_template_index(self):
"""
The admin/index.html template uses block.super in the bodyclass block.