Opened 3 months ago

Closed 3 months ago

#35755 closed Bug (fixed)

Help text for hidden fields is visible in admin fieldsets

Reported by: Richard Laager Owned by: Richard Laager
Component: contrib.admin Version: 4.2
Severity: Normal Keywords:
Cc: Tom Carrick Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: yes

Description

This is present in 4.2 through git main.

If a field is hidden, its help_text shows.

This regressed in commit 96a598356a9ea8c2c05b22cadc12e256a3b295fd:
https://github.com/django/django/commit/96a598356a9ea8c2c05b22cadc12e256a3b295fd

from PR 16161:
https://github.com/django/django/pull/16161

This happened because the <div class="help"> is now after, as opposed to inside, the <div> that gets class "hidden".

There are two possible fixes:

A) Do not output the help div at all:

--- a/django/contrib/admin/templates/admin/includes/fieldset.html
+++ b/django/contrib/admin/templates/admin/includes/fieldset.html
@@ -26,7 +26,7 @@
                                 {% endif %}
                             {% endif %}
                         </div>
-                    {% if field.field.help_text %}
+                    {% if field.field.help_text and not field.field.is_hidden %}
                         <div class="help"{% if field.field.id_for_label %} id="{{ field.field.id_for_label }}_helptext"{% endif %}>
                             <div>{{ field.field.help_text|safe }}</div>
                         </div>

B) Set "hidden" on the help div:

--- a/django/contrib/admin/templates/admin/includes/fieldset.html
+++ b/django/contrib/admin/templates/admin/includes/fieldset.html
@@ -27,7 +27,7 @@
                             {% endif %}
                         </div>
                     {% if field.field.help_text %}
-                        <div class="help"{% if field.field.id_for_label %} id="{{ field.field.id_for_label }}_helptext"{% endif %}>
+                        <div class="help{% if field.field.is_hidden %} hidden{% endif %}"{% if field.field.id_for_label %} id="{{ field.field.id_for_label }}_helptext"{% endif %}>
                             <div>{{ field.field.help_text|safe }}</div>
                         </div>
                     {% endif %}

Either fix works. I'm just not sure stylistically which one you want.

Change History (5)

comment:1 by Richard Laager, 3 months ago

Easy pickings: set
Has patch: set

comment:2 by Sarah Boyce, 3 months ago

Cc: Tom Carrick added
Has patch: unset
Summary: Regression: Help text for hidden fields showsHelp text for hidden fields is visible in admin fieldsets
Triage Stage: UnreviewedAccepted
UI/UX: set

Thank you Richard! Would you like to prepare a PR?

On what is the "right" way to fix it, I think either approach works, I would perhaps add the "hidden" class.

As a rough idea of a regression test, something like this might work (depending on the approach):

  • tests/admin_inlines/models.py

    a b class SomeParentModel(models.Model):  
    332332
    333333class SomeChildModel(models.Model):
    334334    name = models.CharField(max_length=1)
    335     position = models.PositiveIntegerField()
     335    position = models.PositiveIntegerField(help_text="Position help_text.")
    336336    parent = models.ForeignKey(SomeParentModel, models.CASCADE)
    337337    readonly_field = models.CharField(max_length=1)
    338338
  • tests/admin_inlines/tests.py

    diff --git a/tests/admin_inlines/tests.py b/tests/admin_inlines/tests.py
    index cba8db83d7..d74d6cbf04 100644
    a b class TestInline(TestDataMixin, TestCase):  
    383383            response.rendered_content,
    384384        )
    385385        self.assertInHTML(
    386             '<div class="flex-container fieldBox field-position hidden">'
    387             '<label class="inline">Position:</label>'
    388             '<div class="readonly">1</div></div>',
     386            '<div class="help hidden"><div>Position help_text.</div></div>',
    389387            response.rendered_content,
    390388        )

comment:3 by Richard Laager, 3 months ago

Has patch: set
Owner: set to Richard Laager
Status: newassigned

comment:4 by Sarah Boyce, 3 months ago

Triage Stage: AcceptedReady for checkin

comment:5 by Sarah Boyce <42296566+sarahboyce@…>, 3 months ago

Resolution: fixed
Status: assignedclosed

In 16af0c60:

Fixed #35755 -- Hid help text of hidden fields in admin fieldsets.

Regression in 96a598356a9ea8c2c05b22cadc12e256a3b295fd.

Note: See TracTickets for help on using tickets.
Back to Top