2012-06-11 08:34:00 +00:00
|
|
|
==============
|
|
|
|
Editing mixins
|
|
|
|
==============
|
|
|
|
|
2012-07-16 00:30:39 +00:00
|
|
|
The following mixins are used to construct Django's editing views:
|
|
|
|
|
|
|
|
* :class:`django.views.generic.edit.FormMixin`
|
|
|
|
* :class:`django.views.generic.edit.ModelFormMixin`
|
|
|
|
* :class:`django.views.generic.edit.ProcessFormView`
|
|
|
|
* :class:`django.views.generic.edit.DeletionMixin`
|
|
|
|
|
2012-08-04 20:01:40 +00:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Examples of how these are combined into editing views can be found at
|
2014-02-09 10:50:09 +00:00
|
|
|
the documentation on :doc:`/ref/class-based-views/generic-editing`.
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``FormMixin``
|
|
|
|
=============
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. class:: django.views.generic.edit.FormMixin
|
|
|
|
|
|
|
|
A mixin class that provides facilities for creating and displaying forms.
|
|
|
|
|
2015-02-03 13:41:57 +00:00
|
|
|
**Mixins**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.base.ContextMixin`
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
**Methods and Attributes**
|
|
|
|
|
|
|
|
.. attribute:: initial
|
|
|
|
|
|
|
|
A dictionary containing initial data for the form.
|
|
|
|
|
|
|
|
.. attribute:: form_class
|
|
|
|
|
|
|
|
The form class to instantiate.
|
|
|
|
|
|
|
|
.. attribute:: success_url
|
|
|
|
|
|
|
|
The URL to redirect to when the form is successfully processed.
|
|
|
|
|
2013-06-22 11:12:43 +00:00
|
|
|
.. attribute:: prefix
|
|
|
|
|
2013-06-25 11:50:43 +00:00
|
|
|
The :attr:`~django.forms.Form.prefix` for the generated form.
|
2013-06-22 11:12:43 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. method:: get_initial()
|
|
|
|
|
|
|
|
Retrieve initial data for the form. By default, returns a copy of
|
|
|
|
:attr:`~django.views.generic.edit.FormMixin.initial`.
|
|
|
|
|
|
|
|
.. method:: get_form_class()
|
|
|
|
|
|
|
|
Retrieve the form class to instantiate. By default
|
|
|
|
:attr:`.form_class`.
|
|
|
|
|
2014-10-14 18:56:39 +00:00
|
|
|
.. method:: get_form(form_class=None)
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
Instantiate an instance of ``form_class`` using
|
|
|
|
:meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
|
2014-10-14 18:56:39 +00:00
|
|
|
If ``form_class`` isn't provided :meth:`get_form_class` will be used.
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. method:: get_form_kwargs()
|
|
|
|
|
|
|
|
Build the keyword arguments required to instantiate the form.
|
|
|
|
|
|
|
|
The ``initial`` argument is set to :meth:`.get_initial`. If the
|
|
|
|
request is a ``POST`` or ``PUT``, the request data (``request.POST``
|
|
|
|
and ``request.FILES``) will also be provided.
|
|
|
|
|
2013-06-25 11:50:43 +00:00
|
|
|
.. method:: get_prefix()
|
|
|
|
|
|
|
|
Determine the :attr:`~django.forms.Form.prefix` for the generated form.
|
|
|
|
Returns :attr:`~django.views.generic.edit.FormMixin.prefix` by default.
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. method:: get_success_url()
|
|
|
|
|
|
|
|
Determine the URL to redirect to when the form is successfully
|
|
|
|
validated. Returns
|
|
|
|
:attr:`~django.views.generic.edit.FormMixin.success_url` by default.
|
|
|
|
|
|
|
|
.. method:: form_valid(form)
|
|
|
|
|
|
|
|
Redirects to
|
|
|
|
:meth:`~django.views.generic.edit.FormMixin.get_success_url`.
|
|
|
|
|
|
|
|
.. method:: form_invalid(form)
|
|
|
|
|
|
|
|
Renders a response, providing the invalid form as context.
|
|
|
|
|
2015-04-14 20:50:36 +00:00
|
|
|
.. method:: get_context_data(**kwargs)
|
|
|
|
|
|
|
|
Calls :meth:`get_form` and adds the result to the context data with the
|
|
|
|
name 'form'.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``ModelFormMixin``
|
|
|
|
==================
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. class:: django.views.generic.edit.ModelFormMixin
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
A form mixin that works on ``ModelForms``, rather than a standalone form.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
Since this is a subclass of
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectMixin`, instances of this
|
2013-01-01 13:12:42 +00:00
|
|
|
mixin have access to the
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectMixin.model` and
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectMixin.queryset` attributes,
|
2014-02-09 10:50:09 +00:00
|
|
|
describing the type of object that the ``ModelForm`` is manipulating.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2014-11-15 11:17:55 +00:00
|
|
|
If you specify both the
|
|
|
|
:attr:`~django.views.generic.edit.ModelFormMixin.fields` and
|
|
|
|
:attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
|
|
|
|
:exc:`~django.core.exceptions.ImproperlyConfigured` exception will be
|
|
|
|
raised.
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
**Mixins**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.edit.FormMixin`
|
|
|
|
* :class:`django.views.generic.detail.SingleObjectMixin`
|
|
|
|
|
|
|
|
**Methods and Attributes**
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
.. attribute:: model
|
|
|
|
|
|
|
|
A model class. Can be explicitly provided, otherwise will be determined
|
|
|
|
by examining ``self.object`` or
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectMixin.queryset`.
|
|
|
|
|
2013-02-21 21:56:55 +00:00
|
|
|
.. attribute:: fields
|
|
|
|
|
|
|
|
A list of names of fields. This is interpreted the same way as the
|
|
|
|
``Meta.fields`` attribute of :class:`~django.forms.ModelForm`.
|
|
|
|
|
|
|
|
This is a required attribute if you are generating the form class
|
|
|
|
automatically (e.g. using ``model``). Omitting this attribute will
|
2014-03-22 00:44:34 +00:00
|
|
|
result in an :exc:`~django.core.exceptions.ImproperlyConfigured`
|
|
|
|
exception.
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. attribute:: success_url
|
|
|
|
|
|
|
|
The URL to redirect to when the form is successfully processed.
|
|
|
|
|
|
|
|
``success_url`` may contain dictionary string formatting, which
|
|
|
|
will be interpolated against the object's field attributes. For
|
2015-01-12 11:00:27 +00:00
|
|
|
example, you could use ``success_url="/polls/{slug}/"`` to
|
2012-06-11 08:34:00 +00:00
|
|
|
redirect to a URL composed out of the ``slug`` field on a model.
|
|
|
|
|
|
|
|
.. method:: get_form_class()
|
|
|
|
|
|
|
|
Retrieve the form class to instantiate. If
|
2013-01-01 13:12:42 +00:00
|
|
|
:attr:`~django.views.generic.edit.FormMixin.form_class` is provided,
|
|
|
|
that class will be used. Otherwise, a ``ModelForm`` will be
|
|
|
|
instantiated using the model associated with the
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectMixin.queryset`, or
|
|
|
|
with the :attr:`~django.views.generic.detail.SingleObjectMixin.model`,
|
|
|
|
depending on which attribute is provided.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
.. method:: get_form_kwargs()
|
|
|
|
|
|
|
|
Add the current instance (``self.object``) to the standard
|
2013-01-01 13:12:42 +00:00
|
|
|
:meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
.. method:: get_success_url()
|
|
|
|
|
|
|
|
Determine the URL to redirect to when the form is successfully
|
2013-01-01 13:12:42 +00:00
|
|
|
validated. Returns
|
|
|
|
:attr:`django.views.generic.edit.ModelFormMixin.success_url` if it is
|
|
|
|
provided; otherwise, attempts to use the ``get_absolute_url()`` of the
|
|
|
|
object.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
.. method:: form_valid(form)
|
|
|
|
|
|
|
|
Saves the form instance, sets the current object for the view, and
|
|
|
|
redirects to
|
|
|
|
:meth:`~django.views.generic.edit.FormMixin.get_success_url`.
|
|
|
|
|
|
|
|
.. method:: form_invalid()
|
|
|
|
|
|
|
|
Renders a response, providing the invalid form as context.
|
2012-08-11 06:07:15 +00:00
|
|
|
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``ProcessFormView``
|
|
|
|
===================
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. class:: django.views.generic.edit.ProcessFormView
|
|
|
|
|
|
|
|
A mixin that provides basic HTTP GET and POST workflow.
|
|
|
|
|
2012-08-11 06:07:15 +00:00
|
|
|
.. note::
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
This is named 'ProcessFormView' and inherits directly from
|
|
|
|
:class:`django.views.generic.base.View`, but breaks if used
|
|
|
|
independently, so it is more of a mixin.
|
|
|
|
|
|
|
|
**Extends**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
**Methods and Attributes**
|
|
|
|
|
|
|
|
.. method:: get(request, *args, **kwargs)
|
|
|
|
|
2015-04-14 20:50:36 +00:00
|
|
|
Renders a response using a context created with
|
|
|
|
:meth:`~django.views.generic.edit.FormMixin.get_context_data`.
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. method:: post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
Constructs a form, checks the form for validity, and handles it
|
|
|
|
accordingly.
|
|
|
|
|
2013-03-29 11:57:07 +00:00
|
|
|
.. method:: put(*args, **kwargs)
|
|
|
|
|
|
|
|
The ``PUT`` action is also handled and just passes all parameters
|
|
|
|
through to :meth:`post`.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2014-02-09 10:50:09 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``DeletionMixin``
|
|
|
|
=================
|
2014-02-09 10:50:09 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. class:: django.views.generic.edit.DeletionMixin
|
|
|
|
|
|
|
|
Enables handling of the ``DELETE`` http action.
|
|
|
|
|
|
|
|
**Methods and Attributes**
|
|
|
|
|
|
|
|
.. attribute:: success_url
|
|
|
|
|
|
|
|
The url to redirect to when the nominated object has been
|
|
|
|
successfully deleted.
|
|
|
|
|
2014-03-24 15:42:56 +00:00
|
|
|
``success_url`` may contain dictionary string formatting, which will be
|
|
|
|
interpolated against the object's field attributes. For example, you
|
2015-01-12 11:00:27 +00:00
|
|
|
could use ``success_url="/parent/{parent_id}/"`` to redirect to a URL
|
2014-03-24 15:42:56 +00:00
|
|
|
composed out of the ``parent_id`` field on a model.
|
2013-02-11 07:39:14 +00:00
|
|
|
|
2018-05-24 07:57:10 +00:00
|
|
|
.. method:: delete(request, *args, **kwargs)
|
|
|
|
|
|
|
|
Retrieves the target object and calls its ``delete()`` method, then
|
|
|
|
redirects to the success URL.
|
|
|
|
|
2013-02-23 13:52:33 +00:00
|
|
|
.. method:: get_success_url()
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
Returns the url to redirect to when the nominated object has been
|
|
|
|
successfully deleted. Returns
|
|
|
|
:attr:`~django.views.generic.edit.DeletionMixin.success_url` by
|
|
|
|
default.
|