Fixed #24643 -- Added get_context_data() method to FormMixin

This commit is contained in:
Andrei Kulakov 2015-04-14 16:50:36 -04:00 committed by Tim Graham
parent a37dcfd0a3
commit 8a1824d465
5 changed files with 40 additions and 8 deletions

View File

@ -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):
"""

View File

@ -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`

View File

@ -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)

View File

@ -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()
<django.views.generic.edit.ProcessFormView.get>` method to the new
:meth:`FormMixin.get_context_data()
<django.views.generic.edit.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

View File

@ -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):