mixins-editing.txt 7.8 KB

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