mixins-editing.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. ==============
  2. Editing mixins
  3. ==============
  4. The following mixins are used to construct Django's editing views:
  5. * :class:`django.views.generic.edit.FormMixin`
  6. * :class:`django.views.generic.edit.ModelFormMixin`
  7. * :class:`django.views.generic.edit.ProcessFormView`
  8. * :class:`django.views.generic.edit.DeletionMixin`
  9. .. note::
  10. Examples of how these are combined into editing views can be found at
  11. the documentation on :doc:`/ref/class-based-views/generic-editing`.
  12. FormMixin
  13. ---------
  14. .. class:: django.views.generic.edit.FormMixin
  15. A mixin class that provides facilities for creating and displaying forms.
  16. **Mixins**
  17. * :class:`django.views.generic.base.ContextMixin`
  18. **Methods and Attributes**
  19. .. attribute:: initial
  20. A dictionary containing initial data for the form.
  21. .. attribute:: form_class
  22. The form class to instantiate.
  23. .. attribute:: success_url
  24. The URL to redirect to when the form is successfully processed.
  25. .. attribute:: prefix
  26. The :attr:`~django.forms.Form.prefix` for the generated form.
  27. .. method:: get_initial()
  28. Retrieve initial data for the form. By default, returns a copy of
  29. :attr:`~django.views.generic.edit.FormMixin.initial`.
  30. .. method:: get_form_class()
  31. Retrieve the form class to instantiate. By default
  32. :attr:`.form_class`.
  33. .. method:: get_form(form_class=None)
  34. Instantiate an instance of ``form_class`` using
  35. :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
  36. If ``form_class`` isn't provided :meth:`get_form_class` will be used.
  37. .. versionchanged:: 1.8
  38. The ``form_class`` argument is not required anymore.
  39. .. method:: get_form_kwargs()
  40. Build the keyword arguments required to instantiate the form.
  41. The ``initial`` argument is set to :meth:`.get_initial`. If the
  42. request is a ``POST`` or ``PUT``, the request data (``request.POST``
  43. and ``request.FILES``) will also be provided.
  44. .. method:: get_prefix()
  45. Determine the :attr:`~django.forms.Form.prefix` for the generated form.
  46. Returns :attr:`~django.views.generic.edit.FormMixin.prefix` by default.
  47. .. method:: get_success_url()
  48. Determine the URL to redirect to when the form is successfully
  49. validated. Returns
  50. :attr:`~django.views.generic.edit.FormMixin.success_url` by default.
  51. .. method:: form_valid(form)
  52. Redirects to
  53. :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
  54. .. method:: form_invalid(form)
  55. Renders a response, providing the invalid form as context.
  56. .. method:: get_context_data(**kwargs)
  57. .. versionadded:: 1.9
  58. Calls :meth:`get_form` and adds the result to the context data with the
  59. name 'form'.
  60. ModelFormMixin
  61. --------------
  62. .. class:: django.views.generic.edit.ModelFormMixin
  63. A form mixin that works on ``ModelForms``, rather than a standalone form.
  64. Since this is a subclass of
  65. :class:`~django.views.generic.detail.SingleObjectMixin`, instances of this
  66. mixin have access to the
  67. :attr:`~django.views.generic.detail.SingleObjectMixin.model` and
  68. :attr:`~django.views.generic.detail.SingleObjectMixin.queryset` attributes,
  69. describing the type of object that the ``ModelForm`` is manipulating.
  70. If you specify both the
  71. :attr:`~django.views.generic.edit.ModelFormMixin.fields` and
  72. :attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
  73. :exc:`~django.core.exceptions.ImproperlyConfigured` exception will be
  74. raised.
  75. .. versionchanged:: 1.8
  76. Previously if both ``fields`` and ``form_class`` were specified,
  77. ``fields`` was silently ignored.
  78. **Mixins**
  79. * :class:`django.views.generic.edit.FormMixin`
  80. * :class:`django.views.generic.detail.SingleObjectMixin`
  81. **Methods and Attributes**
  82. .. attribute:: model
  83. A model class. Can be explicitly provided, otherwise will be determined
  84. by examining ``self.object`` or
  85. :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`.
  86. .. attribute:: fields
  87. A list of names of fields. This is interpreted the same way as the
  88. ``Meta.fields`` attribute of :class:`~django.forms.ModelForm`.
  89. This is a required attribute if you are generating the form class
  90. automatically (e.g. using ``model``). Omitting this attribute will
  91. result in an :exc:`~django.core.exceptions.ImproperlyConfigured`
  92. exception.
  93. .. versionchanged:: 1.8
  94. Previously, omitting this attribute was allowed and resulted in
  95. a form with all fields of the model.
  96. .. attribute:: success_url
  97. The URL to redirect to when the form is successfully processed.
  98. ``success_url`` may contain dictionary string formatting, which
  99. will be interpolated against the object's field attributes. For
  100. example, you could use ``success_url="/polls/{slug}/"`` to
  101. redirect to a URL composed out of the ``slug`` field on a model.
  102. .. versionchanged:: 1.8
  103. Support for the new brace-based Python formatting syntax has been
  104. added. The old ``%(slug)s`` placeholder syntax support has been
  105. deprecated and will be removed in Django 1.10.
  106. .. method:: get_form_class()
  107. Retrieve the form class to instantiate. If
  108. :attr:`~django.views.generic.edit.FormMixin.form_class` is provided,
  109. that class will be used. Otherwise, a ``ModelForm`` will be
  110. instantiated using the model associated with the
  111. :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`, or
  112. with the :attr:`~django.views.generic.detail.SingleObjectMixin.model`,
  113. depending on which attribute is provided.
  114. .. method:: get_form_kwargs()
  115. Add the current instance (``self.object``) to the standard
  116. :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
  117. .. method:: get_success_url()
  118. Determine the URL to redirect to when the form is successfully
  119. validated. Returns
  120. :attr:`django.views.generic.edit.ModelFormMixin.success_url` if it is
  121. provided; otherwise, attempts to use the ``get_absolute_url()`` of the
  122. object.
  123. .. method:: form_valid(form)
  124. Saves the form instance, sets the current object for the view, and
  125. redirects to
  126. :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
  127. .. method:: form_invalid()
  128. Renders a response, providing the invalid form as context.
  129. ProcessFormView
  130. ---------------
  131. .. class:: django.views.generic.edit.ProcessFormView
  132. A mixin that provides basic HTTP GET and POST workflow.
  133. .. note::
  134. This is named 'ProcessFormView' and inherits directly from
  135. :class:`django.views.generic.base.View`, but breaks if used
  136. independently, so it is more of a mixin.
  137. **Extends**
  138. * :class:`django.views.generic.base.View`
  139. **Methods and Attributes**
  140. .. method:: get(request, *args, **kwargs)
  141. Renders a response using a context created with
  142. :meth:`~django.views.generic.edit.FormMixin.get_context_data`.
  143. .. versionchanged:: 1.9
  144. Construction of the form was moved from this method to
  145. :meth:`~django.views.generic.edit.FormMixin.get_context_data`.
  146. .. method:: post(request, *args, **kwargs)
  147. Constructs a form, checks the form for validity, and handles it
  148. accordingly.
  149. .. method:: put(*args, **kwargs)
  150. The ``PUT`` action is also handled and just passes all parameters
  151. through to :meth:`post`.
  152. DeletionMixin
  153. -------------
  154. .. class:: django.views.generic.edit.DeletionMixin
  155. Enables handling of the ``DELETE`` http action.
  156. **Methods and Attributes**
  157. .. attribute:: success_url
  158. The url to redirect to when the nominated object has been
  159. successfully deleted.
  160. ``success_url`` may contain dictionary string formatting, which will be
  161. interpolated against the object's field attributes. For example, you
  162. could use ``success_url="/parent/{parent_id}/"`` to redirect to a URL
  163. composed out of the ``parent_id`` field on a model.
  164. .. versionchanged:: 1.8
  165. Support for the new brace-based Python formatting syntax has been
  166. added. The old ``%(slug)s`` placeholder syntax support has been
  167. deprecated and will be removed in Django 1.10.
  168. .. method:: get_success_url()
  169. Returns the url to redirect to when the nominated object has been
  170. successfully deleted. Returns
  171. :attr:`~django.views.generic.edit.DeletionMixin.success_url` by
  172. default.