summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Selivanov <seroy@bk.ru>2017-07-19 18:24:27 +0300
committerTim Graham <timograham@gmail.com>2017-07-19 14:28:06 -0400
commitd4da39685b5974849c73e4c4dc6e07dfdf21c67a (patch)
tree0da3ca91d2f198e010b79af6150c1fbcc0485d85
parent3f7953846ee2e60a2cd56d6dea3b0ba9ffaa76c5 (diff)
Fixed #28414 -- Fixed ClearableFileInput rendering as a subwidget of MultiWidget.
-rw-r--r--django/contrib/admin/templates/admin/widgets/clearable_file_input.html10
-rw-r--r--django/forms/jinja2/django/forms/widgets/clearable_file_input.html8
-rw-r--r--django/forms/templates/django/forms/widgets/clearable_file_input.html8
-rw-r--r--django/forms/widgets.py2
-rw-r--r--docs/releases/1.11.4.txt7
-rw-r--r--docs/spelling_wordlist1
-rw-r--r--tests/forms_tests/widget_tests/test_clearablefileinput.py14
7 files changed, 35 insertions, 15 deletions
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 %}<p class="file-upload">{{ initial_text }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
+{% 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="{{ checkbox_name }}" id="{{ checkbox_id }}" />
-<label for="{{ checkbox_id }}">{{ clear_checkbox_label }}</label></span>{% endif %}<br />
-{{ input_text }}:{% endif %}
-<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %} />{% if is_initial %}</p>{% endif %}
+<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}" />
+<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 %}
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 }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
-<input type="checkbox" name="{{ checkbox_name }}" id="{{ checkbox_id }}" />
-<label for="{{ checkbox_id }}">{{ clear_checkbox_label }}</label>{% endif %}<br />
-{{ input_text }}:{% endif %}
+{% 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 }}" />
+<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 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 }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
-<input type="checkbox" name="{{ checkbox_name }}" id="{{ checkbox_id }}" />
-<label for="{{ checkbox_id }}">{{ clear_checkbox_label }}</label>{% endif %}<br />
-{{ input_text }}:{% endif %}
+{% 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 }}" />
+<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/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='<input type="file" name="myfile" />')
+ 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: <a href="something">something</a>
+ <input type="checkbox" name="myfile_0-clear" id="myfile_0-clear_id" />
+ <label for="myfile_0-clear_id">Clear</label><br />
+ Change: <input type="file" name="myfile_0" />
+ """
+ ))
+
def test_clear_input_checked_returns_false(self):
"""
ClearableFileInput.value_from_datadict returns False if the clear