diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index f832011..1dc0354 100644
a
|
b
|
from itertools import chain
|
9 | 9 | import warnings |
10 | 10 | |
11 | 11 | from django.conf import settings |
| 12 | from django.core.files.uploadedfile import UploadedFile |
12 | 13 | from django.forms.utils import flatatt, to_current_timezone |
13 | 14 | from django.utils.datastructures import MultiValueDict, MergeDict |
14 | 15 | from django.utils.html import conditional_escape, format_html |
… |
… |
class ClearableFileInput(FileInput):
|
324 | 325 | input_text = ugettext_lazy('Change') |
325 | 326 | clear_checkbox_label = ugettext_lazy('Clear') |
326 | 327 | |
327 | | template_with_initial = '%(initial_text)s: %(initial)s %(clear_template)s<br />%(input_text)s: %(input)s' |
| 328 | template_with_initial = '%(initial_text)s: <a href="%(initial_url)s">%(initial)s</a> %(clear_template)s<br />%(input_text)s: %(input)s' |
328 | 329 | |
329 | 330 | template_with_clear = '%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label>' |
330 | 331 | |
331 | | url_markup_template = '<a href="{0}">{1}</a>' |
332 | | |
333 | 332 | def clear_checkbox_name(self, name): |
334 | 333 | """ |
335 | 334 | Given the name of the file input, return the name of the clear checkbox |
… |
… |
class ClearableFileInput(FileInput):
|
343 | 342 | """ |
344 | 343 | return name + '_id' |
345 | 344 | |
| 345 | def is_initial(self, value): |
| 346 | """ |
| 347 | Returns whether value is considered to be initial value. |
| 348 | """ |
| 349 | return bool(value and hasattr(value, 'url')) |
| 350 | |
| 351 | def value_to_substitutions(self, value): |
| 352 | """ |
| 353 | Returns value-related substitutions. |
| 354 | """ |
| 355 | return { |
| 356 | 'initial': conditional_escape(value), |
| 357 | 'initial_url': conditional_escape(value.url) |
| 358 | } |
| 359 | |
346 | 360 | def render(self, name, value, attrs=None): |
347 | 361 | substitutions = { |
348 | 362 | 'initial_text': self.initial_text, |
… |
… |
class ClearableFileInput(FileInput):
|
353 | 367 | template = '%(input)s' |
354 | 368 | substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs) |
355 | 369 | |
356 | | if value and hasattr(value, "url"): |
| 370 | if self.is_initial(value): |
357 | 371 | template = self.template_with_initial |
358 | | substitutions['initial'] = format_html(self.url_markup_template, |
359 | | value.url, |
360 | | force_text(value)) |
| 372 | substitutions.update(self.value_to_substitutions(value)) |
361 | 373 | if not self.is_required: |
362 | 374 | checkbox_name = self.clear_checkbox_name(name) |
363 | 375 | checkbox_id = self.clear_checkbox_id(checkbox_name) |