mixins-editing.txt 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 ``Generic editing views``.
  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. .. versionadded:: 1.6
  25. The :attr:`~django.forms.Form.prefix` for the generated form.
  26. .. method:: get_initial()
  27. Retrieve initial data for the form. By default, returns a copy of
  28. :attr:`~django.views.generic.edit.FormMixin.initial`.
  29. .. method:: get_form_class()
  30. Retrieve the form class to instantiate. By default
  31. :attr:`.form_class`.
  32. .. method:: get_form(form_class)
  33. Instantiate an instance of ``form_class`` using
  34. :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
  35. .. method:: get_form_kwargs()
  36. Build the keyword arguments required to instantiate the form.
  37. The ``initial`` argument is set to :meth:`.get_initial`. If the
  38. request is a ``POST`` or ``PUT``, the request data (``request.POST``
  39. and ``request.FILES``) will also be provided.
  40. .. method:: get_prefix()
  41. .. versionadded:: 1.6
  42. Determine the :attr:`~django.forms.Form.prefix` for the generated form.
  43. Returns :attr:`~django.views.generic.edit.FormMixin.prefix` by default.
  44. .. method:: get_success_url()
  45. Determine the URL to redirect to when the form is successfully
  46. validated. Returns
  47. :attr:`~django.views.generic.edit.FormMixin.success_url` by default.
  48. .. method:: form_valid(form)
  49. Redirects to
  50. :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
  51. .. method:: form_invalid(form)
  52. Renders a response, providing the invalid form as context.
  53. .. method:: get_context_data(**kwargs)
  54. Populates a context containing the contents of ``kwargs``.
  55. **Context**
  56. * ``form``: The form instance that was generated for the view.
  57. .. note::
  58. Views mixing ``FormMixin`` must provide an implementation of
  59. :meth:`form_valid` and :meth:`form_invalid`.
  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. The
  70. view also provides ``self.object``, the instance being manipulated. If the
  71. instance is being created, ``self.object`` will be ``None``.
  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. .. versionadded:: 1.6
  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 all fields being used, but this behavior is deprecated
  87. and will be removed in Django 1.8.
  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)s/"`` 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()
  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. Constructs a form, then renders a response using a context that
  130. contains that form.
  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 just passes all parameters
  136. through to :meth:`post`.
  137. .. class:: django.views.generic.edit.DeletionMixin
  138. Enables handling of the ``DELETE`` http action.
  139. **Methods and Attributes**
  140. .. attribute:: success_url
  141. The url to redirect to when the nominated object has been
  142. successfully deleted.
  143. .. versionadded:: 1.6
  144. ``success_url`` may contain dictionary string formatting, which
  145. will be interpolated against the object's field attributes. For
  146. example, you could use ``success_url="/parent/%(parent_id)s/"`` to
  147. redirect to a URL composed out of the ``parent_id`` field on a model.
  148. .. method:: get_success_url()
  149. Returns the url to redirect to when the nominated object has been
  150. successfully deleted. Returns
  151. :attr:`~django.views.generic.edit.DeletionMixin.success_url` by
  152. default.