From 3d7235c67b5b0569890411eeba8db2b1e02c89c4 Mon Sep 17 00:00:00 2001 From: Johannes Maron Date: Thu, 14 Mar 2024 13:43:05 +0100 Subject: [PATCH] Refs #34488 -- Made ClearableFileInput preserve "Clear" checked attribute when form is invalid. --- .../forms/widgets/clearable_file_input.html | 2 +- .../forms/widgets/clearable_file_input.html | 2 +- .../widget_tests/test_clearablefileinput.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/django/forms/jinja2/django/forms/widgets/clearable_file_input.html b/django/forms/jinja2/django/forms/widgets/clearable_file_input.html index 93812837f0..4f3a93627f 100644 --- a/django/forms/jinja2/django/forms/widgets/clearable_file_input.html +++ b/django/forms/jinja2/django/forms/widgets/clearable_file_input.html @@ -1,5 +1,5 @@ {% if widget.is_initial %}{{ widget.initial_text }}: {{ widget.value }}{% if not widget.required %} - + {% endif %}
{{ widget.input_text }}:{% endif %} diff --git a/django/forms/templates/django/forms/widgets/clearable_file_input.html b/django/forms/templates/django/forms/widgets/clearable_file_input.html index 93812837f0..4f3a93627f 100644 --- a/django/forms/templates/django/forms/widgets/clearable_file_input.html +++ b/django/forms/templates/django/forms/widgets/clearable_file_input.html @@ -1,5 +1,5 @@ {% if widget.is_initial %}{{ widget.initial_text }}: {{ widget.value }}{% if not widget.required %} - + {% endif %}
{{ widget.input_text }}:{% endif %} diff --git a/tests/forms_tests/widget_tests/test_clearablefileinput.py b/tests/forms_tests/widget_tests/test_clearablefileinput.py index 53b84cc0ec..ae54cc4b5d 100644 --- a/tests/forms_tests/widget_tests/test_clearablefileinput.py +++ b/tests/forms_tests/widget_tests/test_clearablefileinput.py @@ -111,6 +111,21 @@ class ClearableFileInputTest(WidgetTest): ), ) + def test_render_checked(self): + self.widget.checked = True + self.check_html( + self.widget, + "myfile", + FakeFieldFile(), + html=( + 'Currently: something' + '" + '
Change: ' + '' + ), + ) + def test_render_no_disabled(self): class TestForm(Form): clearable_file = FileField(