#35439 closed Cleanup/optimization (wontfix)
Hardcoded HTML in python code.
Reported by: | sesostris | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 5.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
There is a hardcoded snippet of HTML in the django.contrib.admin.templatetags.admin_list module on lines 99 to 110 that will be used in the header of the changelist results table.
# if the field is the action checkbox: no sorting and special class if field_name == "action_checkbox": aria_label = _("Select all objects on this page for an action") yield { "text": mark_safe( f'<input type="checkbox" id="action-toggle" ' f'aria-label="{aria_label}">' ), "class_attrib": mark_safe(' class="action-checkbox-column"'), "sortable": False, } continue
It would be better to use a CheckboxInput widget to render this HTML element. The code would look like this:
from django.forms import CheckboxInput # if the field is the action checkbox: no sorting and special class if field_name == "action_checkbox": widget = CheckboxInput( attrs={ "aria-label": _( "Select all objects on this page for an action" ), "id": "action-toggle", } ) yield { "text": mark_safe( widget.render(name="action-toggle", value=False) ), "class_attrib": mark_safe(' class="action-checkbox-column"'), "sortable": False, } continue
Change History (2)
comment:1 by , 6 months ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
comment:2 by , 6 months ago
I would also like to read the answer to the last question (why using a widget and rending the HTML using the template engine would be better) but is worth nothing that the following diff has the test passing OK so at a code level it seems equivalent:
-
django/contrib/admin/templatetags/admin_list.py
diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py index fdf6e63f5f..e7b4fcb81f 100644
a b 1 1 import datetime 2 2 3 from django.forms import CheckboxInput 3 4 from django.contrib.admin.templatetags.admin_urls import add_preserved_filters 4 5 from django.contrib.admin.utils import ( 5 6 display_for_field, … … def result_headers(cl): 99 100 # if the field is the action checkbox: no sorting and special class 100 101 if field_name == "action_checkbox": 101 102 aria_label = _("Select all objects on this page for an action") 103 widget = CheckboxInput( 104 attrs={ 105 "aria-label": aria_label, 106 "id": "action-toggle", 107 } 108 ) 102 109 yield { 103 110 "text": mark_safe( 104 f'<input type="checkbox" id="action-toggle" ' 105 f'aria-label="{aria_label}">' 111 widget.render(name="action-toggle", value=False) 106 112 ), 107 113 "class_attrib": mark_safe(' class="action-checkbox-column"'), 108 114 "sortable": False, -
tests/admin_changelist/tests.py
diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index bf85cf038f..bf6245ca1f 100644
a b class ChangeListTests(TestCase): 359 359 "Failed to find expected row element: %s" % table_output, 360 360 ) 361 361 self.assertInHTML( 362 '<input type="checkbox" id="action-toggle" '362 '<input type="checkbox" id="action-toggle" name="action-toggle"' 363 363 'aria-label="Select all objects on this page for an action">', 364 364 table_output, 365 365 )
Thank you for the ticket.
I'm not sure it's worth changing as there are test failures with this change so it is not equivalent. The ticket mentions that using a widget would be better but I'm unsure why using a widget and rending the HTML using the template engine would be better in this particular case.