From 8a1824d465a551b44376f17c01ef142cc036e863 Mon Sep 17 00:00:00 2001 From: Andrei Kulakov Date: Tue, 14 Apr 2015 16:50:36 -0400 Subject: [PATCH] Fixed #24643 -- Added get_context_data() method to FormMixin --- django/views/generic/edit.py | 12 +++++++++--- docs/ref/class-based-views/flattened-index.txt | 6 +++--- docs/ref/class-based-views/mixins-editing.txt | 16 ++++++++++++++-- docs/releases/1.9.txt | 7 +++++++ tests/generic_views/test_edit.py | 7 +++++++ 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py index 2a2590263f1..3d3ff45686f 100644 --- a/django/views/generic/edit.py +++ b/django/views/generic/edit.py @@ -112,7 +112,14 @@ class FormMixin(six.with_metaclass(FormMixinBase, ContextMixin)): If the form is invalid, re-render the context data with the data-filled form and errors. """ - return self.render_to_response(self.get_context_data(form=form)) + return self.render_to_response(self.get_context_data()) + + def get_context_data(self, **kwargs): + """ + Insert the form into the context dict. + """ + kwargs.setdefault('form', self.get_form()) + return super(FormMixin, self).get_context_data(**kwargs) class ModelFormMixin(FormMixin, SingleObjectMixin): @@ -202,8 +209,7 @@ class ProcessFormView(View): """ Handles GET requests and instantiates a blank version of the form. """ - form = self.get_form() - return self.render_to_response(self.get_context_data(form=form)) + return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): """ diff --git a/docs/ref/class-based-views/flattened-index.txt b/docs/ref/class-based-views/flattened-index.txt index 9bc5270ae09..9107d43d8b8 100644 --- a/docs/ref/class-based-views/flattened-index.txt +++ b/docs/ref/class-based-views/flattened-index.txt @@ -168,7 +168,7 @@ FormView * :meth:`~django.views.generic.edit.FormMixin.form_invalid` * :meth:`~django.views.generic.edit.FormMixin.form_valid` * :meth:`~django.views.generic.edit.ProcessFormView.get` -* :meth:`~django.views.generic.base.ContextMixin.get_context_data` +* :meth:`~django.views.generic.edit.FormMixin.get_context_data` * :meth:`~django.views.generic.edit.FormMixin.get_form` * :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs` * :meth:`~django.views.generic.base.View.http_method_not_allowed` @@ -207,7 +207,7 @@ CreateView * :meth:`~django.views.generic.edit.FormMixin.form_invalid` * :meth:`~django.views.generic.edit.ModelFormMixin.form_valid` * :meth:`~django.views.generic.edit.ProcessFormView.get` -* :meth:`~django.views.generic.base.ContextMixin.get_context_data` +* :meth:`~django.views.generic.edit.FormMixin.get_context_data` * :meth:`~django.views.generic.edit.FormMixin.get_form` * :meth:`~django.views.generic.edit.ModelFormMixin.get_form_kwargs` * :meth:`~django.views.generic.detail.SingleObjectMixin.get_object` @@ -249,7 +249,7 @@ UpdateView * :meth:`~django.views.generic.edit.FormMixin.form_invalid` * :meth:`~django.views.generic.edit.ModelFormMixin.form_valid` * :meth:`~django.views.generic.edit.ProcessFormView.get` -* :meth:`~django.views.generic.base.ContextMixin.get_context_data` +* :meth:`~django.views.generic.edit.FormMixin.get_context_data` * :meth:`~django.views.generic.edit.FormMixin.get_form` * :meth:`~django.views.generic.edit.ModelFormMixin.get_form_kwargs` * :meth:`~django.views.generic.detail.SingleObjectMixin.get_object` diff --git a/docs/ref/class-based-views/mixins-editing.txt b/docs/ref/class-based-views/mixins-editing.txt index 024b89cd2b8..a5cbfcc437d 100644 --- a/docs/ref/class-based-views/mixins-editing.txt +++ b/docs/ref/class-based-views/mixins-editing.txt @@ -91,6 +91,13 @@ FormMixin Renders a response, providing the invalid form as context. + .. method:: get_context_data(**kwargs) + + .. versionadded:: 1.9 + + Calls :meth:`get_form` and adds the result to the context data with the + name 'form'. + ModelFormMixin -------------- @@ -214,8 +221,13 @@ ProcessFormView .. method:: get(request, *args, **kwargs) - Constructs a form, then renders a response using a context that - contains that form. + Renders a response using a context created with + :meth:`~django.views.generic.edit.FormMixin.get_context_data`. + + .. versionchanged:: 1.9 + + Construction of the form was moved from this method to + :meth:`~django.views.generic.edit.FormMixin.get_context_data`. .. method:: post(request, *args, **kwargs) diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt index 7db2e4618f9..3c2a35b1496 100644 --- a/docs/releases/1.9.txt +++ b/docs/releases/1.9.txt @@ -407,6 +407,13 @@ Miscellaneous * ``CommaSeparatedIntegerField`` validation has been refined to forbid values like ``','``, ``',1'``, and ``'1,,2'``. +* Form initialization was moved from the :meth:`ProcessFormView.get() + ` method to the new + :meth:`FormMixin.get_context_data() + ` method. This may be + backwards incompatible if you have overridden the ``get_context_data()`` + method without calling ``super()``. + .. _deprecated-features-1.9: Features deprecated in 1.9 diff --git a/tests/generic_views/test_edit.py b/tests/generic_views/test_edit.py index a48c84cf27d..64e1759a269 100644 --- a/tests/generic_views/test_edit.py +++ b/tests/generic_views/test_edit.py @@ -81,6 +81,13 @@ class FormMixinTests(TestCase): MissingDefaultValue().get_form(), forms.Form, ) + def test_get_context_data(self): + class FormContext(FormMixin): + request = RequestFactory().get('/') + form_class = forms.Form + + self.assertIsInstance(FormContext().get_context_data()['form'], forms.Form) + @override_settings(ROOT_URLCONF='generic_views.urls') class BasicFormTests(TestCase):