Commits

Carl Meyer committed c0ab59f

fix problem with rendering bound form with ClearableFileField; fixes #8

  • Participants
  • Parent commits e77957b

Comments (0)

Files changed (3)

 tip (unreleased)
 ----------------
 
+- Fix error redisplaying bound form with ClearableFileField.
+
 - Automatically set ``fields`` on ``BetterModelForm`` to list of fields
   present in ``fieldsets``, if ``fields`` or ``exclude`` are not set
   manually.

File form_utils/widgets.py

 """
 widgets for django-form-utils
 
-Time-stamp: <2010-03-05 15:03:36 carljm widgets.py>
-
 parts of this code taken from http://www.djangosnippets.org/snippets/934/
  - thanks baumer1122
 
             widgets=[file_widget, forms.CheckboxInput()],
             attrs=attrs)
 
+    def render(self, name, value, attrs=None):
+        if isinstance(value, list):
+            self.value = value[0]
+        else:
+            self.value = value
+        return super(ClearableFileInput, self).render(name, value, attrs)
+        
     def decompress(self, value):
         # the clear checkbox is never initially checked
-        self.value = value
         return [value, None]
 
     def format_output(self, rendered_widgets):

File tests/tests.py

 
 class ClearableFileFieldTests(TestCase):
     upload = SimpleUploadedFile('something.txt', 'Something')
+
+    def test_bound_redisplay(self):
+        class TestForm(forms.Form):
+            f = ClearableFileField()
+        form = TestForm(files={'f_0': self.upload})
+        self.assertEquals(unicode(form['f']),
+                          u'<input type="file" name="f_0" id="id_f_0" />'
+                          u' Clear: <input type="checkbox" name="f_1" id="id_f_1" />')
     
     def test_not_cleared(self):
         """