Commits

Branko Vukelic  committed 71538b8

Fixed form not honoring initial on GET

  • Participants
  • Parent commits 43d403d

Comments (0)

Files changed (2)

File related/views.py

         context = dict()
 
         if self.get_existing_form_class():
-            context['existing_form'] = self.get_existing_form(
-                data=self.request.GET
-            )
+            if self.request.method == 'GET' and self.request.GET:
+                form = self.get_existing_form(data=self.request.GET)
+            else:
+                form = self.get_existing_form()
+
+            context['existing_form'] = form
 
         context.update(
             super(GetExistingMixin, self).get_context_data(*arg, **kwarg)

File tests/tests.py

 
         self.assertTrue(ctx.has_key('existing_form'))
 
+    def test_setting_initial_when_GET_is_empty(self):
+        class Super(object):
+            def get_context_data(self):
+                return dict(foo='bar')
+
+        class View(GetExistingMixin, Super):
+            pass
+
+        view = View()
+        view.request = Mock()
+        view.request.method = 'GET'
+        view.request.GET = dict()
+        view.get_existing_form_class = Mock()
+        view.get_existing_form = Mock()
+
+        ctx = view.get_context_data()
+
+        self.assertEqual(ctx['existing_form'],
+                         view.get_existing_form.return_value)
+        view.get_existing_form.assert_called_once_with()
+
+    def test_setting_initial_when_GET_is_not_empty(self):
+        class Super(object):
+            def get_context_data(self):
+                return dict(foo='bar')
+
+        class View(GetExistingMixin, Super):
+            pass
+
+        view = View()
+        view.request = Mock()
+        view.request.method = 'GET'
+        view.request.GET = dict(fam='dam')
+        view.get_existing_form_class = Mock()
+        view.get_existing_form = Mock()
+
+        ctx = view.get_context_data()
+
+        view.get_existing_form.assert_called_once_with(data=dict(fam='dam'))
+
     def test_without_form(self):
         class Super(object):
             def get_context_data(self):