Loading django/forms/widgets.py +18 −7 Original line number Diff line number Diff line Loading @@ -341,12 +341,10 @@ class ClearableFileInput(FileInput): input_text = ugettext_lazy('Change') clear_checkbox_label = ugettext_lazy('Clear') template_with_initial = '%(initial_text)s: %(initial)s %(clear_template)s<br />%(input_text)s: %(input)s' template_with_initial = '%(initial_text)s: <a href="%(initial_url)s">%(initial)s</a> %(clear_template)s<br />%(input_text)s: %(input)s' template_with_clear = '%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label>' url_markup_template = '<a href="{0}">{1}</a>' def clear_checkbox_name(self, name): """ Given the name of the file input, return the name of the clear checkbox Loading @@ -360,6 +358,21 @@ class ClearableFileInput(FileInput): """ return name + '_id' def is_initial(self, value): """ Return whether value is considered to be initial value. """ return bool(value and hasattr(value, 'url')) def get_template_substitution_values(self, value): """ Return value-related substitutions. """ return { 'initial': conditional_escape(value), 'initial_url': conditional_escape(value.url), } def render(self, name, value, attrs=None): substitutions = { 'initial_text': self.initial_text, Loading @@ -370,11 +383,9 @@ class ClearableFileInput(FileInput): template = '%(input)s' substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs) if value and hasattr(value, "url"): if self.is_initial(value): template = self.template_with_initial substitutions['initial'] = format_html(self.url_markup_template, value.url, force_text(value)) substitutions.update(self.get_template_substitution_values(value)) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) Loading docs/releases/1.8.txt +4 −0 Original line number Diff line number Diff line Loading @@ -522,6 +522,10 @@ Miscellaneous opposed to the instance name which you can still customize using ``AdminSite(name="...")``. * Interal changes were made to the :class:`~django.forms.ClearableFileInput` widget to allow more customization. The undocumented ``url_markup_template`` attribute was removed in favor of ``template_with_initial``. .. _deprecated-features-1.8: Features deprecated in 1.8 Loading tests/forms_tests/tests/test_widgets.py +13 −0 Original line number Diff line number Diff line Loading @@ -1249,3 +1249,16 @@ class ClearableFileInputTests(TestCase): data={'myfile-clear': True}, files={'myfile': f}, name='myfile'), f) def test_render_custom_template(self): widget = ClearableFileInput() widget.template_with_initial = ( '%(initial_text)s: <img src="%(initial_url)s" alt="%(initial)s" /> ' '%(clear_template)s<br />%(input_text)s: %(input)s' ) self.assertHTMLEqual( widget.render('myfile', FakeFieldFile()), 'Currently: <img src="something" alt="something" /> ' '<input type="checkbox" name="myfile-clear" id="myfile-clear_id" /> ' '<label for="myfile-clear_id">Clear</label><br />Change: <input type="file" name="myfile" />' ) Loading
django/forms/widgets.py +18 −7 Original line number Diff line number Diff line Loading @@ -341,12 +341,10 @@ class ClearableFileInput(FileInput): input_text = ugettext_lazy('Change') clear_checkbox_label = ugettext_lazy('Clear') template_with_initial = '%(initial_text)s: %(initial)s %(clear_template)s<br />%(input_text)s: %(input)s' template_with_initial = '%(initial_text)s: <a href="%(initial_url)s">%(initial)s</a> %(clear_template)s<br />%(input_text)s: %(input)s' template_with_clear = '%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label>' url_markup_template = '<a href="{0}">{1}</a>' def clear_checkbox_name(self, name): """ Given the name of the file input, return the name of the clear checkbox Loading @@ -360,6 +358,21 @@ class ClearableFileInput(FileInput): """ return name + '_id' def is_initial(self, value): """ Return whether value is considered to be initial value. """ return bool(value and hasattr(value, 'url')) def get_template_substitution_values(self, value): """ Return value-related substitutions. """ return { 'initial': conditional_escape(value), 'initial_url': conditional_escape(value.url), } def render(self, name, value, attrs=None): substitutions = { 'initial_text': self.initial_text, Loading @@ -370,11 +383,9 @@ class ClearableFileInput(FileInput): template = '%(input)s' substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs) if value and hasattr(value, "url"): if self.is_initial(value): template = self.template_with_initial substitutions['initial'] = format_html(self.url_markup_template, value.url, force_text(value)) substitutions.update(self.get_template_substitution_values(value)) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) Loading
docs/releases/1.8.txt +4 −0 Original line number Diff line number Diff line Loading @@ -522,6 +522,10 @@ Miscellaneous opposed to the instance name which you can still customize using ``AdminSite(name="...")``. * Interal changes were made to the :class:`~django.forms.ClearableFileInput` widget to allow more customization. The undocumented ``url_markup_template`` attribute was removed in favor of ``template_with_initial``. .. _deprecated-features-1.8: Features deprecated in 1.8 Loading
tests/forms_tests/tests/test_widgets.py +13 −0 Original line number Diff line number Diff line Loading @@ -1249,3 +1249,16 @@ class ClearableFileInputTests(TestCase): data={'myfile-clear': True}, files={'myfile': f}, name='myfile'), f) def test_render_custom_template(self): widget = ClearableFileInput() widget.template_with_initial = ( '%(initial_text)s: <img src="%(initial_url)s" alt="%(initial)s" /> ' '%(clear_template)s<br />%(input_text)s: %(input)s' ) self.assertHTMLEqual( widget.render('myfile', FakeFieldFile()), 'Currently: <img src="something" alt="something" /> ' '<input type="checkbox" name="myfile-clear" id="myfile-clear_id" /> ' '<label for="myfile-clear_id">Clear</label><br />Change: <input type="file" name="myfile" />' )