summaryrefslogtreecommitdiff
path: root/django/contrib/admin/helpers.py
diff options
context:
space:
mode:
authorMarijke Luttekes <mail@marijkeluttekes.dev>2024-05-20 14:39:09 -0300
committernessita <124304+nessita@users.noreply.github.com>2024-05-22 00:13:55 -0300
commite4a693f50a8342ed1a54b74e1a202b44c8e62981 (patch)
treefe5924414b2edafaec2ee4c7709cbf86dfc94220 /django/contrib/admin/helpers.py
parent01ed59f753139afb514170ee7f7384c155ecbc2d (diff)
Fixed #35189 -- Improved admin collapsible fieldsets by using <details> elements.
This work improves the accessibility of the add and change pages in the admin site by adding <details> and <summary> elements to the collapsible fieldsets. This has the nice side effect of no longer requiring custom JavaScript helpers to implement the fieldsets' show/hide capabilities. Thanks to James Scholes for the accessibility advice, and to Sarah Boyce and Tom Carrick for reviews. Co-authored-by: Natalia <124304+nessita@users.noreply.github.com> Co-authored-by: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com>
Diffstat (limited to 'django/contrib/admin/helpers.py')
-rw-r--r--django/contrib/admin/helpers.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py
index c4613fa24e..a4aa8e40e3 100644
--- a/django/contrib/admin/helpers.py
+++ b/django/contrib/admin/helpers.py
@@ -18,6 +18,7 @@ from django.db.models.fields.related import (
from django.forms.utils import flatatt
from django.template.defaultfilters import capfirst, linebreaksbr
from django.urls import NoReverseMatch, reverse
+from django.utils.functional import cached_property
from django.utils.html import conditional_escape, format_html
from django.utils.safestring import mark_safe
from django.utils.translation import gettext
@@ -116,10 +117,14 @@ class Fieldset:
@property
def media(self):
- if "collapse" in self.classes:
- return forms.Media(js=["admin/js/collapse.js"])
return forms.Media()
+ @cached_property
+ def is_collapsible(self):
+ if any([field in self.fields for field in self.form.errors]):
+ return False
+ return "collapse" in self.classes
+
def __iter__(self):
for field in self.fields:
yield Fieldline(
@@ -438,6 +443,12 @@ class InlineAdminFormSet:
def forms(self):
return self.formset.forms
+ @cached_property
+ def is_collapsible(self):
+ if any(self.formset.errors):
+ return False
+ return "collapse" in self.classes
+
def non_form_errors(self):
return self.formset.non_form_errors()