Kaydet (Commit) d4da3968 authored tarafından Roman Selivanov's avatar Roman Selivanov Kaydeden (comit) Tim Graham

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

üst 3f795384
{% 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 %}
{% 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" %} />
{% 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" %} />
...@@ -392,7 +392,7 @@ class ClearableFileInput(FileInput): ...@@ -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),
......
...@@ -28,3 +28,10 @@ Bugfixes ...@@ -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.
...@@ -696,6 +696,7 @@ subtransactions ...@@ -696,6 +696,7 @@ subtransactions
subtree subtree
subtype subtype
subviews subviews
subwidget
subwidgets subwidgets
superclass superclass
superset superset
......
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): ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment