2
0

mixins-editing.txt 7.7 KB

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