Fixed #28414 -- Fixed ClearableFileInput rendering as a subwidget of MultiWidget.

This commit is contained in:
Roman Selivanov 2017-07-19 18:24:27 +03:00 committed by Tim Graham
parent 3f7953846e
commit d4da39685b
7 changed files with 35 additions and 15 deletions

View File

@ -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"> <span class="clearable-file-input">
<input type="checkbox" name="{{ checkbox_name }}" id="{{ checkbox_id }}" /> <input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}" />
<label for="{{ checkbox_id }}">{{ clear_checkbox_label }}</label></span>{% endif %}<br /> <label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label></span>{% endif %}<br />
{{ input_text }}:{% endif %} {{ widget.input_text }}:{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %} />{% if is_initial %}</p>{% endif %} <input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %} />{% if widget.is_initial %}</p>{% endif %}

View File

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

View File

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

View File

@ -392,7 +392,7 @@ class ClearableFileInput(FileInput):
context = super().get_context(name, value, attrs) context = super().get_context(name, value, attrs)
checkbox_name = self.clear_checkbox_name(name) checkbox_name = self.clear_checkbox_name(name)
checkbox_id = self.clear_checkbox_id(checkbox_name) checkbox_id = self.clear_checkbox_id(checkbox_name)
context.update({ context['widget'].update({
'checkbox_name': checkbox_name, 'checkbox_name': checkbox_name,
'checkbox_id': checkbox_id, 'checkbox_id': checkbox_id,
'is_initial': self.is_initial(value), 'is_initial': self.is_initial(value),

View File

@ -28,3 +28,10 @@ Bugfixes
* Fixed ``QuerySet.count()`` for ``union()``, ``difference()``, and * Fixed ``QuerySet.count()`` for ``union()``, ``difference()``, and
``intersection()`` queries. (:ticket:`28399`). ``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.

View File

@ -696,6 +696,7 @@ subtransactions
subtree subtree
subtype subtype
subviews subviews
subwidget
subwidgets subwidgets
superclass superclass
superset superset

View File

@ -1,5 +1,5 @@
from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files.uploadedfile import SimpleUploadedFile
from django.forms import ClearableFileInput from django.forms import ClearableFileInput, MultiWidget
from .base import WidgetTest from .base import WidgetTest
@ -74,6 +74,18 @@ class ClearableFileInputTest(WidgetTest):
""" """
self.check_html(self.widget, 'myfile', None, html='<input type="file" name="myfile" />') 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): def test_clear_input_checked_returns_false(self):
""" """
ClearableFileInput.value_from_datadict returns False if the clear ClearableFileInput.value_from_datadict returns False if the clear