Opened 6 months ago

Closed 6 months ago

Last modified 6 months ago

#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.

https://github.com/django/django/blob/0e445badd54fafc75dd1a5dff9fee6e6a171eafe/django/contrib/admin/templatetags/admin_list.py#L99C1-L110C25

            # 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 David Smith, 6 months ago

Resolution: wontfix
Status: newclosed

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.

comment:2 by Natalia Bidart, 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  
    11import datetime
    22
     3from django.forms import CheckboxInput
    34from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
    45from django.contrib.admin.utils import (
    56    display_for_field,
    def result_headers(cl):  
    99100            # if the field is the action checkbox: no sorting and special class
    100101            if field_name == "action_checkbox":
    101102                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                )
    102109                yield {
    103110                    "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)
    106112                    ),
    107113                    "class_attrib": mark_safe(' class="action-checkbox-column"'),
    108114                    "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):  
    359359            "Failed to find expected row element: %s" % table_output,
    360360        )
    361361        self.assertInHTML(
    362             '<input type="checkbox" id="action-toggle" '
     362            '<input type="checkbox" id="action-toggle" name="action-toggle"'
    363363            'aria-label="Select all objects on this page for an action">',
    364364            table_output,
    365365        )
Note: See TracTickets for help on using tickets.
Back to Top