mixins-editing.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. **Mixins**
  72. * :class:`django.views.generic.edit.FormMixin`
  73. * :class:`django.views.generic.detail.SingleObjectMixin`
  74. **Methods and Attributes**
  75. .. attribute:: model
  76. A model class. Can be explicitly provided, otherwise will be determined
  77. by examining ``self.object`` or
  78. :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`.
  79. .. attribute:: fields
  80. A list of names of fields. This is interpreted the same way as the
  81. ``Meta.fields`` attribute of :class:`~django.forms.ModelForm`.
  82. This is a required attribute if you are generating the form class
  83. automatically (e.g. using ``model``). Omitting this attribute will
  84. result in an :exc:`~django.core.exceptions.ImproperlyConfigured`
  85. exception.
  86. .. versionchanged:: 1.8
  87. Previously, omitting this attribute was allowed and resulted in
  88. a form with all fields of the model.
  89. .. attribute:: success_url
  90. The URL to redirect to when the form is successfully processed.
  91. ``success_url`` may contain dictionary string formatting, which
  92. will be interpolated against the object's field attributes. For
  93. example, you could use ``success_url="/polls/%(slug)s/"`` to
  94. redirect to a URL composed out of the ``slug`` field on a model.
  95. .. method:: get_form_class()
  96. Retrieve the form class to instantiate. If
  97. :attr:`~django.views.generic.edit.FormMixin.form_class` is provided,
  98. that class will be used. Otherwise, a ``ModelForm`` will be
  99. instantiated using the model associated with the
  100. :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`, or
  101. with the :attr:`~django.views.generic.detail.SingleObjectMixin.model`,
  102. depending on which attribute is provided.
  103. .. method:: get_form_kwargs()
  104. Add the current instance (``self.object``) to the standard
  105. :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
  106. .. method:: get_success_url()
  107. Determine the URL to redirect to when the form is successfully
  108. validated. Returns
  109. :attr:`django.views.generic.edit.ModelFormMixin.success_url` if it is
  110. provided; otherwise, attempts to use the ``get_absolute_url()`` of the
  111. object.
  112. .. method:: form_valid(form)
  113. Saves the form instance, sets the current object for the view, and
  114. redirects to
  115. :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
  116. .. method:: form_invalid()
  117. Renders a response, providing the invalid form as context.
  118. ProcessFormView
  119. ---------------
  120. .. class:: django.views.generic.edit.ProcessFormView
  121. A mixin that provides basic HTTP GET and POST workflow.
  122. .. note::
  123. This is named 'ProcessFormView' and inherits directly from
  124. :class:`django.views.generic.base.View`, but breaks if used
  125. independently, so it is more of a mixin.
  126. **Extends**
  127. * :class:`django.views.generic.base.View`
  128. **Methods and Attributes**
  129. .. method:: get(request, *args, **kwargs)
  130. Constructs a form, then renders a response using a context that
  131. contains that form.
  132. .. method:: post(request, *args, **kwargs)
  133. Constructs a form, checks the form for validity, and handles it
  134. accordingly.
  135. .. method:: put(*args, **kwargs)
  136. The ``PUT`` action is also handled and just passes all parameters
  137. through to :meth:`post`.
  138. DeletionMixin
  139. -------------
  140. .. class:: django.views.generic.edit.DeletionMixin
  141. Enables handling of the ``DELETE`` http action.
  142. **Methods and Attributes**
  143. .. attribute:: success_url
  144. The url to redirect to when the nominated object has been
  145. successfully deleted.
  146. ``success_url`` may contain dictionary string formatting, which will be
  147. interpolated against the object's field attributes. For example, you
  148. could use ``success_url="/parent/%(parent_id)s/"`` to redirect to a URL
  149. composed out of the ``parent_id`` field on a model.
  150. .. method:: get_success_url()
  151. Returns the url to redirect to when the nominated object has been
  152. successfully deleted. Returns
  153. :attr:`~django.views.generic.edit.DeletionMixin.success_url` by
  154. default.