diff --git a/django/contrib/admin/templates/admin/widgets/clearable_file_input.html b/django/contrib/admin/templates/admin/widgets/clearable_file_input.html index 9fee235819..71491fca45 100644 --- a/django/contrib/admin/templates/admin/widgets/clearable_file_input.html +++ b/django/contrib/admin/templates/admin/widgets/clearable_file_input.html @@ -1,6 +1,6 @@ -{% if is_initial %}

{{ initial_text }}: {{ widget.value }}{% if not widget.required %} +{% if widget.is_initial %}

{{ widget.initial_text }}: {{ widget.value }}{% if not widget.required %} - -{% endif %}
-{{ input_text }}:{% endif %} -{% if is_initial %}

{% endif %} + +{% endif %}
+{{ widget.input_text }}:{% endif %} +{% if widget.is_initial %}

{% endif %} 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 05f2c2dbe5..7248f32d2a 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 is_initial %}{{ initial_text }}: {{ widget.value }}{% if not widget.required %} - -{% endif %}
-{{ input_text }}:{% endif %} +{% 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 05f2c2dbe5..7248f32d2a 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 is_initial %}{{ initial_text }}: {{ widget.value }}{% if not widget.required %} - -{% endif %}
-{{ input_text }}:{% endif %} +{% if widget.is_initial %}{{ widget.initial_text }}: {{ widget.value }}{% if not widget.required %} + +{% endif %}
+{{ widget.input_text }}:{% endif %} diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 599d1e8011..01ada423c9 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -392,7 +392,7 @@ class ClearableFileInput(FileInput): context = super().get_context(name, value, attrs) checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) - context.update({ + context['widget'].update({ 'checkbox_name': checkbox_name, 'checkbox_id': checkbox_id, 'is_initial': self.is_initial(value), diff --git a/docs/releases/1.11.4.txt b/docs/releases/1.11.4.txt index 8952ce1c4b..180d758246 100644 --- a/docs/releases/1.11.4.txt +++ b/docs/releases/1.11.4.txt @@ -28,3 +28,10 @@ Bugfixes * Fixed ``QuerySet.count()`` for ``union()``, ``difference()``, and ``intersection()`` queries. (:ticket:`28399`). + +* Fixed ``ClearableFileInput`` rendering as a subwidget of ``MultiWidget`` + (:ticket:`28414`). Custom ``clearable_file_input.html`` widget templates + will need to adapt for the fact that context values + ``checkbox_name``, ``checkbox_id``, ``is_initial``, ``input_text``, + ``initial_text``, and ``clear_checkbox_label`` are now attributes of + ``widget`` rather than appearing in the top-level context. diff --git a/docs/spelling_wordlist b/docs/spelling_wordlist index afbb16e514..ed3570657b 100644 --- a/docs/spelling_wordlist +++ b/docs/spelling_wordlist @@ -696,6 +696,7 @@ subtransactions subtree subtype subviews +subwidget subwidgets superclass superset diff --git a/tests/forms_tests/widget_tests/test_clearablefileinput.py b/tests/forms_tests/widget_tests/test_clearablefileinput.py index 09307e9556..f338269d09 100644 --- a/tests/forms_tests/widget_tests/test_clearablefileinput.py +++ b/tests/forms_tests/widget_tests/test_clearablefileinput.py @@ -1,5 +1,5 @@ from django.core.files.uploadedfile import SimpleUploadedFile -from django.forms import ClearableFileInput +from django.forms import ClearableFileInput, MultiWidget from .base import WidgetTest @@ -74,6 +74,18 @@ class ClearableFileInputTest(WidgetTest): """ self.check_html(self.widget, 'myfile', None, html='') + def test_render_as_subwidget(self): + """A ClearableFileInput as a subwidget of MultiWidget.""" + widget = MultiWidget(widgets=(self.widget,)) + self.check_html(widget, 'myfile', [FakeFieldFile()], html=( + """ + Currently: something + +
+ Change: + """ + )) + def test_clear_input_checked_returns_false(self): """ ClearableFileInput.value_from_datadict returns False if the clear