summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Maron <johannes@maron.family>2024-03-14 13:43:05 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2024-03-14 14:03:14 +0100
commit3d7235c67b5b0569890411eeba8db2b1e02c89c4 (patch)
treeb2b29a7a16c63bbff104c6c74ded590b572c944c
parente69019555d683fd6a831f87cb09e3deb86e4e7c7 (diff)
Refs #34488 -- Made ClearableFileInput preserve "Clear" checked attribute when form is invalid.
-rw-r--r--django/forms/jinja2/django/forms/widgets/clearable_file_input.html2
-rw-r--r--django/forms/templates/django/forms/widgets/clearable_file_input.html2
-rw-r--r--tests/forms_tests/widget_tests/test_clearablefileinput.py15
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 }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
-<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}>
+<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>{% endif %}<br>
{{ widget.input_text }}:{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
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 }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
-<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}>
+<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>{% endif %}<br>
{{ widget.input_text }}:{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
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: <a href="something">something</a>'
+ '<input type="checkbox" name="myfile-clear" id="myfile-clear_id" '
+ "checked>"
+ '<label for="myfile-clear_id">Clear</label><br>Change: '
+ '<input type="file" name="myfile" checked>'
+ ),
+ )
+
def test_render_no_disabled(self):
class TestForm(Form):
clearable_file = FileField(