mixins-editing.txt 6.1 KB

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