mixins-editing.txt 8.0 KB

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