mixins-editing.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. .. method:: get_initial()
  24. Retrieve initial data for the form. By default, returns a copy of
  25. :attr:`~django.views.generic.edit.FormMixin.initial`.
  26. .. method:: get_form_class()
  27. Retrieve the form class to instantiate. By default
  28. :attr:`.form_class`.
  29. .. method:: get_form(form_class)
  30. Instantiate an instance of ``form_class`` using
  31. :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
  32. .. method:: get_form_kwargs()
  33. Build the keyword arguments required to instantiate the form.
  34. The ``initial`` argument is set to :meth:`.get_initial`. If the
  35. request is a ``POST`` or ``PUT``, the request data (``request.POST``
  36. and ``request.FILES``) will also be provided.
  37. .. method:: get_success_url()
  38. Determine the URL to redirect to when the form is successfully
  39. validated. Returns
  40. :attr:`~django.views.generic.edit.FormMixin.success_url` by default.
  41. .. method:: form_valid(form)
  42. Redirects to
  43. :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
  44. .. method:: form_invalid(form)
  45. Renders a response, providing the invalid form as context.
  46. .. method:: get_context_data(**kwargs)
  47. Populates a context containing the contents of ``kwargs``.
  48. **Context**
  49. * ``form``: The form instance that was generated for the view.
  50. .. note::
  51. Views mixing ``FormMixin`` must provide an implementation of
  52. :meth:`form_valid` and :meth:`form_invalid`.
  53. ModelFormMixin
  54. --------------
  55. .. class:: django.views.generic.edit.ModelFormMixin
  56. A form mixin that works on ``ModelForms``, rather than a standalone form.
  57. Since this is a subclass of
  58. :class:`~django.views.generic.detail.SingleObjectMixin`, instances of this
  59. mixin have access to the
  60. :attr:`~django.views.generic.detail.SingleObjectMixin.model` and
  61. :attr:`~django.views.generic.detail.SingleObjectMixin.queryset` attributes,
  62. describing the type of object that the ``ModelForm`` is manipulating. The
  63. view also provides ``self.object``, the instance being manipulated. If the
  64. instance is being created, ``self.object`` will be ``None``.
  65. **Mixins**
  66. * :class:`django.views.generic.edit.FormMixin`
  67. * :class:`django.views.generic.detail.SingleObjectMixin`
  68. **Methods and Attributes**
  69. .. attribute:: model
  70. A model class. Can be explicitly provided, otherwise will be determined
  71. by examining ``self.object`` or
  72. :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`.
  73. .. attribute:: success_url
  74. The URL to redirect to when the form is successfully processed.
  75. ``success_url`` may contain dictionary string formatting, which
  76. will be interpolated against the object's field attributes. For
  77. example, you could use ``success_url="/polls/%(slug)s/"`` to
  78. redirect to a URL composed out of the ``slug`` field on a model.
  79. .. method:: get_form_class()
  80. Retrieve the form class to instantiate. If
  81. :attr:`~django.views.generic.edit.FormMixin.form_class` is provided,
  82. that class will be used. Otherwise, a ``ModelForm`` will be
  83. instantiated using the model associated with the
  84. :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`, or
  85. with the :attr:`~django.views.generic.detail.SingleObjectMixin.model`,
  86. depending on which attribute is provided.
  87. .. method:: get_form_kwargs()
  88. Add the current instance (``self.object``) to the standard
  89. :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
  90. .. method:: get_success_url()
  91. Determine the URL to redirect to when the form is successfully
  92. validated. Returns
  93. :attr:`django.views.generic.edit.ModelFormMixin.success_url` if it is
  94. provided; otherwise, attempts to use the ``get_absolute_url()`` of the
  95. object.
  96. .. method:: form_valid(form)
  97. Saves the form instance, sets the current object for the view, and
  98. redirects to
  99. :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
  100. .. method:: form_invalid()
  101. Renders a response, providing the invalid form as context.
  102. ProcessFormView
  103. ---------------
  104. .. class:: django.views.generic.edit.ProcessFormView
  105. A mixin that provides basic HTTP GET and POST workflow.
  106. .. note::
  107. This is named 'ProcessFormView' and inherits directly from
  108. :class:`django.views.generic.base.View`, but breaks if used
  109. independently, so it is more of a mixin.
  110. **Extends**
  111. * :class:`django.views.generic.base.View`
  112. **Methods and Attributes**
  113. .. method:: get(request, *args, **kwargs)
  114. Constructs a form, then renders a response using a context that
  115. contains that form.
  116. .. method:: post(request, *args, **kwargs)
  117. Constructs a form, checks the form for validity, and handles it
  118. accordingly.
  119. The PUT action is also handled, as an analog of POST.
  120. .. class:: django.views.generic.edit.DeletionMixin
  121. Enables handling of the ``DELETE`` http action.
  122. **Methods and Attributes**
  123. .. attribute:: success_url
  124. The url to redirect to when the nominated object has been
  125. successfully deleted.
  126. .. method:: get_success_url(obj)
  127. Returns the url to redirect to when the nominated object has been
  128. successfully deleted. Returns
  129. :attr:`~django.views.generic.edit.DeletionMixin.success_url` by
  130. default.