1. ==============
    
  2. Editing mixins
    
  3. ==============
    
  4. 
    
  5. The following mixins are used to construct Django's editing views:
    
  6. 
    
  7. * :class:`django.views.generic.edit.FormMixin`
    
  8. * :class:`django.views.generic.edit.ModelFormMixin`
    
  9. * :class:`django.views.generic.edit.ProcessFormView`
    
  10. * :class:`django.views.generic.edit.DeletionMixin`
    
  11. 
    
  12. .. note::
    
  13. 
    
  14.     Examples of how these are combined into editing views can be found at
    
  15.     the documentation on :doc:`/ref/class-based-views/generic-editing`.
    
  16. 
    
  17. ``FormMixin``
    
  18. =============
    
  19. 
    
  20. .. class:: django.views.generic.edit.FormMixin
    
  21. 
    
  22.     A mixin class that provides facilities for creating and displaying forms.
    
  23. 
    
  24.     **Mixins**
    
  25. 
    
  26.     * :class:`django.views.generic.base.ContextMixin`
    
  27. 
    
  28.     **Methods and Attributes**
    
  29. 
    
  30.     .. attribute:: initial
    
  31. 
    
  32.         A dictionary containing initial data for the form.
    
  33. 
    
  34.     .. attribute:: form_class
    
  35. 
    
  36.         The form class to instantiate.
    
  37. 
    
  38.     .. attribute:: success_url
    
  39. 
    
  40.         The URL to redirect to when the form is successfully processed.
    
  41. 
    
  42.     .. attribute:: prefix
    
  43. 
    
  44.         The :attr:`~django.forms.Form.prefix` for the generated form.
    
  45. 
    
  46.     .. method:: get_initial()
    
  47. 
    
  48.         Retrieve initial data for the form. By default, returns a copy of
    
  49.         :attr:`~django.views.generic.edit.FormMixin.initial`.
    
  50. 
    
  51.     .. method:: get_form_class()
    
  52. 
    
  53.         Retrieve the form class to instantiate. By default
    
  54.         :attr:`~django.views.generic.edit.FormMixin.form_class`.
    
  55. 
    
  56.     .. method:: get_form(form_class=None)
    
  57. 
    
  58.         Instantiate an instance of ``form_class`` using
    
  59.         :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
    
  60.         If ``form_class`` isn't provided :meth:`get_form_class` will be used.
    
  61. 
    
  62.     .. method:: get_form_kwargs()
    
  63. 
    
  64.         Build the keyword arguments required to instantiate the form.
    
  65. 
    
  66.         The ``initial`` argument is set to :meth:`.get_initial`. If the
    
  67.         request is a ``POST`` or ``PUT``, the request data (``request.POST``
    
  68.         and ``request.FILES``) will also be provided.
    
  69. 
    
  70.     .. method:: get_prefix()
    
  71. 
    
  72.         Determine the :attr:`~django.forms.Form.prefix` for the generated form.
    
  73.         Returns :attr:`~django.views.generic.edit.FormMixin.prefix` by default.
    
  74. 
    
  75.     .. method:: get_success_url()
    
  76. 
    
  77.         Determine the URL to redirect to when the form is successfully
    
  78.         validated. Returns
    
  79.         :attr:`~django.views.generic.edit.FormMixin.success_url` by default.
    
  80. 
    
  81.     .. method:: form_valid(form)
    
  82. 
    
  83.         Redirects to
    
  84.         :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
    
  85. 
    
  86.     .. method:: form_invalid(form)
    
  87. 
    
  88.         Renders a response, providing the invalid form as context.
    
  89. 
    
  90.     .. method:: get_context_data(**kwargs)
    
  91. 
    
  92.         Calls :meth:`get_form` and adds the result to the context data with the
    
  93.         name 'form'.
    
  94. 
    
  95. ``ModelFormMixin``
    
  96. ==================
    
  97. 
    
  98. .. class:: django.views.generic.edit.ModelFormMixin
    
  99. 
    
  100.     A form mixin that works on ``ModelForms``, rather than a standalone form.
    
  101. 
    
  102.     Since this is a subclass of
    
  103.     :class:`~django.views.generic.detail.SingleObjectMixin`, instances of this
    
  104.     mixin have access to the
    
  105.     :attr:`~django.views.generic.detail.SingleObjectMixin.model` and
    
  106.     :attr:`~django.views.generic.detail.SingleObjectMixin.queryset` attributes,
    
  107.     describing the type of object that the ``ModelForm`` is manipulating.
    
  108. 
    
  109.     If you specify both the
    
  110.     :attr:`~django.views.generic.edit.ModelFormMixin.fields` and
    
  111.     :attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
    
  112.     :exc:`~django.core.exceptions.ImproperlyConfigured` exception will be
    
  113.     raised.
    
  114. 
    
  115.     **Mixins**
    
  116. 
    
  117.     * :class:`django.views.generic.edit.FormMixin`
    
  118.     * :class:`django.views.generic.detail.SingleObjectMixin`
    
  119. 
    
  120.     **Methods and Attributes**
    
  121. 
    
  122.     .. attribute:: model
    
  123. 
    
  124.         A model class. Can be explicitly provided, otherwise will be determined
    
  125.         by examining ``self.object`` or
    
  126.         :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`.
    
  127. 
    
  128.     .. attribute:: fields
    
  129. 
    
  130.         A list of names of fields. This is interpreted the same way as the
    
  131.         ``Meta.fields`` attribute of :class:`~django.forms.ModelForm`.
    
  132. 
    
  133.         This is a required attribute if you are generating the form class
    
  134.         automatically (e.g. using ``model``). Omitting this attribute will
    
  135.         result in an :exc:`~django.core.exceptions.ImproperlyConfigured`
    
  136.         exception.
    
  137. 
    
  138.     .. attribute:: success_url
    
  139. 
    
  140.         The URL to redirect to when the form is successfully processed.
    
  141. 
    
  142.         ``success_url`` may contain dictionary string formatting, which
    
  143.         will be interpolated against the object's field attributes. For
    
  144.         example, you could use ``success_url="/polls/{slug}/"`` to
    
  145.         redirect to a URL composed out of the ``slug`` field on a model.
    
  146. 
    
  147.     .. method:: get_form_class()
    
  148. 
    
  149.         Retrieve the form class to instantiate. If
    
  150.         :attr:`~django.views.generic.edit.FormMixin.form_class` is provided,
    
  151.         that class will be used. Otherwise, a ``ModelForm`` will be
    
  152.         instantiated using the model associated with the
    
  153.         :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`, or
    
  154.         with the :attr:`~django.views.generic.detail.SingleObjectMixin.model`,
    
  155.         depending on which attribute is provided.
    
  156. 
    
  157.     .. method:: get_form_kwargs()
    
  158. 
    
  159.         Add the current instance (``self.object``) to the standard
    
  160.         :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
    
  161. 
    
  162.     .. method:: get_success_url()
    
  163. 
    
  164.         Determine the URL to redirect to when the form is successfully
    
  165.         validated. Returns
    
  166.         :attr:`django.views.generic.edit.ModelFormMixin.success_url` if it is
    
  167.         provided; otherwise, attempts to use the ``get_absolute_url()`` of the
    
  168.         object.
    
  169. 
    
  170.     .. method:: form_valid(form)
    
  171. 
    
  172.         Saves the form instance, sets the current object for the view, and
    
  173.         redirects to
    
  174.         :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
    
  175. 
    
  176.     .. method:: form_invalid(form)
    
  177. 
    
  178.         Renders a response, providing the invalid form as context.
    
  179. 
    
  180. 
    
  181. ``ProcessFormView``
    
  182. ===================
    
  183. 
    
  184. .. class:: django.views.generic.edit.ProcessFormView
    
  185. 
    
  186.     A mixin that provides basic HTTP GET and POST workflow.
    
  187. 
    
  188.     .. note::
    
  189. 
    
  190.         This is named 'ProcessFormView' and inherits directly from
    
  191.         :class:`django.views.generic.base.View`, but breaks if used
    
  192.         independently, so it is more of a mixin.
    
  193. 
    
  194.     **Extends**
    
  195. 
    
  196.     * :class:`django.views.generic.base.View`
    
  197. 
    
  198.     **Methods and Attributes**
    
  199. 
    
  200.     .. method:: get(request, *args, **kwargs)
    
  201. 
    
  202.         Renders a response using a context created with
    
  203.         :meth:`~django.views.generic.edit.FormMixin.get_context_data`.
    
  204. 
    
  205.     .. method:: post(request, *args, **kwargs)
    
  206. 
    
  207.         Constructs a form, checks the form for validity, and handles it
    
  208.         accordingly.
    
  209. 
    
  210.     .. method:: put(*args, **kwargs)
    
  211. 
    
  212.         The ``PUT`` action is also handled and passes all parameters through to
    
  213.         :meth:`post`.
    
  214. 
    
  215. 
    
  216. ``DeletionMixin``
    
  217. =================
    
  218. 
    
  219. .. class:: django.views.generic.edit.DeletionMixin
    
  220. 
    
  221.     Enables handling of the ``DELETE`` HTTP action.
    
  222. 
    
  223.     **Methods and Attributes**
    
  224. 
    
  225.     .. attribute:: success_url
    
  226. 
    
  227.         The url to redirect to when the nominated object has been
    
  228.         successfully deleted.
    
  229. 
    
  230.         ``success_url`` may contain dictionary string formatting, which will be
    
  231.         interpolated against the object's field attributes. For example, you
    
  232.         could use ``success_url="/parent/{parent_id}/"`` to redirect to a URL
    
  233.         composed out of the ``parent_id`` field on a model.
    
  234. 
    
  235.     .. method:: delete(request, *args, **kwargs)
    
  236. 
    
  237.         Retrieves the target object and calls its ``delete()`` method, then
    
  238.         redirects to the success URL.
    
  239. 
    
  240.     .. method:: get_success_url()
    
  241. 
    
  242.         Returns the url to redirect to when the nominated object has been
    
  243.         successfully deleted. Returns
    
  244.         :attr:`~django.views.generic.edit.DeletionMixin.success_url` by
    
  245.         default.