2
0
Эх сурвалжийг харах

[5.0.x] Fixed #35273 -- Fixed rendering AdminFileWidget's attributes.

Regression in 8a6c0203c4e92908c2b26ba54feba4ce7e76d081.

Backport of e69019555d683fd6a831f87cb09e3deb86e4e7c7 from main
Johannes Maron 1 жил өмнө
parent
commit
8fd953f28a

+ 1 - 1
django/contrib/admin/templates/admin/widgets/clearable_file_input.html

@@ -1,6 +1,6 @@
 {% if widget.is_initial %}<p class="file-upload">{{ widget.initial_text }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
 <span class="clearable-file-input">
-<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% include "django/forms/widgets/attrs.html" %}>
+<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}{% if widget.attrs.checked %} checked{% endif %}>
 <label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label></span>{% endif %}<br>
 {{ widget.input_text }}:{% endif %}
 <input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% if widget.is_initial %}</p>{% endif %}

+ 4 - 0
docs/releases/5.0.4.txt

@@ -13,3 +13,7 @@ Bugfixes
   fields with expressions in ``db_default``. As a consequence,
   ``Model.full_clean()`` no longer validates for empty values in fields with
   ``db_default`` (:ticket:`35223`).
+
+* Fixed a regression in Django 5.0 where the ``AdminFileWidget`` could be
+  rendered with two ``id`` attributes on the "Clear" checkbox
+  (:ticket:`35273`).

+ 27 - 0
tests/admin_widgets/tests.py

@@ -590,6 +590,19 @@ class AdminFileWidgetTests(TestDataMixin, TestCase):
             '<input type="file" name="test">',
         )
 
+    def test_render_with_attrs_id(self):
+        storage_url = default_storage.url("")
+        w = widgets.AdminFileWidget()
+        self.assertHTMLEqual(
+            w.render("test", self.album.cover_art, attrs={"id": "test_id"}),
+            f'<p class="file-upload">Currently: <a href="{storage_url}albums/'
+            r'hybrid_theory.jpg">albums\hybrid_theory.jpg</a> '
+            '<span class="clearable-file-input">'
+            '<input type="checkbox" name="test-clear" id="test-clear_id"> '
+            '<label for="test-clear_id">Clear</label></span><br>'
+            'Change: <input type="file" name="test" id="test_id"></p>',
+        )
+
     def test_render_required(self):
         widget = widgets.AdminFileWidget()
         widget.is_required = True
@@ -618,6 +631,20 @@ class AdminFileWidgetTests(TestDataMixin, TestCase):
             },
         )
 
+    def test_render_checked(self):
+        storage_url = default_storage.url("")
+        widget = widgets.AdminFileWidget()
+        widget.checked = True
+        self.assertHTMLEqual(
+            widget.render("test", self.album.cover_art),
+            f'<p class="file-upload">Currently: <a href="{storage_url}albums/'
+            r'hybrid_theory.jpg">albums\hybrid_theory.jpg</a> '
+            '<span class="clearable-file-input">'
+            '<input type="checkbox" name="test-clear" id="test-clear_id" checked>'
+            '<label for="test-clear_id">Clear</label></span><br>'
+            'Change: <input type="file" name="test" checked></p>',
+        )
+
     def test_readonly_fields(self):
         """
         File widgets should render as a link when they're marked "read only."