Debian Codegraph @

docs/ref/class-based-views/mixins-editing.txt

raw
1 ==============
2 Editing mixins
3 ==============
4
5 The following mixins are used to construct Django's editing views:
6
7 * :class:`django.views.generic.edit.FormMixin`
8 * :class:`django.views.generic.edit.ModelFormMixin`
9 * :class:`django.views.generic.edit.ProcessFormView`
10 * :class:`django.views.generic.edit.DeletionMixin`
11
12 .. note::
13
14 Examples of how these are combined into editing views can be found at
15 the documentation on :doc:`/ref/class-based-views/generic-editing`.
16
17 ``FormMixin``
18 =============
19
20 .. class:: django.views.generic.edit.FormMixin
21
22 A mixin class that provides facilities for creating and displaying forms.
23
24 **Mixins**
25
26 * :class:`django.views.generic.base.ContextMixin`
27
28 **Methods and Attributes**
29
30 .. attribute:: initial
31
32 A dictionary containing initial data for the form.
33
34 .. attribute:: form_class
35
36 The form class to instantiate.
37
38 .. attribute:: success_url
39
40 The URL to redirect to when the form is successfully processed.
41
42 .. attribute:: prefix
43
44 The :attr:`~django.forms.Form.prefix` for the generated form.
45
46 .. method:: get_initial()
47
48 Retrieve initial data for the form. By default, returns a copy of
49 :attr:`~django.views.generic.edit.FormMixin.initial`.
50
51 .. method:: get_form_class()
52
53 Retrieve the form class to instantiate. By default
54 :attr:`~django.views.generic.edit.FormMixin.form_class`.
55
56 .. method:: get_form(form_class=None)
57
58 Instantiate an instance of ``form_class`` using
59 :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
60 If ``form_class`` isn't provided :meth:`get_form_class` will be used.
61
62 .. method:: get_form_kwargs()
63
64 Build the keyword arguments required to instantiate the form.
65
66 The ``initial`` argument is set to :meth:`.get_initial`. If the
67 request is a ``POST`` or ``PUT``, the request data (``request.POST``
68 and ``request.FILES``) will also be provided.
69
70 .. method:: get_prefix()
71
72 Determine the :attr:`~django.forms.Form.prefix` for the generated form.
73 Returns :attr:`~django.views.generic.edit.FormMixin.prefix` by default.
74
75 .. method:: get_success_url()
76
77 Determine the URL to redirect to when the form is successfully
78 validated. Returns
79 :attr:`~django.views.generic.edit.FormMixin.success_url` by default.
80
81 .. method:: form_valid(form)
82
83 Redirects to
84 :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
85
86 .. method:: form_invalid(form)
87
88 Renders a response, providing the invalid form as context.
89
90 .. method:: get_context_data(**kwargs)
91
92 Calls :meth:`get_form` and adds the result to the context data with the
93 name 'form'.
94
95 ``ModelFormMixin``
96 ==================
97
98 .. class:: django.views.generic.edit.ModelFormMixin
99
100 A form mixin that provides facilities for working with a ``ModelForm``,
101 rather than a standalone form.
102
103 Since this is a subclass of
104 :class:`~django.views.generic.detail.SingleObjectMixin`, instances of this
105 mixin have access to the
106 :attr:`~django.views.generic.detail.SingleObjectMixin.model` and
107 :attr:`~django.views.generic.detail.SingleObjectMixin.queryset` attributes,
108 describing the type of object that the ``ModelForm`` is manipulating.
109
110 If you specify both the
111 :attr:`~django.views.generic.edit.ModelFormMixin.fields` and
112 :attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
113 :exc:`~django.core.exceptions.ImproperlyConfigured` exception will be
114 raised.
115
116 **Mixins**
117
118 * :class:`django.views.generic.edit.FormMixin`
119 * :class:`django.views.generic.detail.SingleObjectMixin`
120
121 **Methods and Attributes**
122
123 .. attribute:: model
124
125 A model class. Can be explicitly provided, otherwise will be determined
126 by examining ``self.object`` or
127 :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`.
128
129 .. attribute:: fields
130
131 A list of names of fields. This is interpreted the same way as the
132 ``Meta.fields`` attribute of :class:`~django.forms.ModelForm`.
133
134 This is a required attribute if you are generating the form class
135 automatically (e.g. using ``model``). Omitting this attribute will
136 result in an :exc:`~django.core.exceptions.ImproperlyConfigured`
137 exception.
138
139 .. attribute:: success_url
140
141 The URL to redirect to when the form is successfully processed.
142
143 ``success_url`` may contain dictionary string formatting, which
144 will be interpolated against the object's field attributes. For
145 example, you could use ``success_url="/polls/{slug}/"`` to
146 redirect to a URL composed out of the ``slug`` field on a model.
147
148 .. method:: get_form_class()
149
150 Retrieve the form class to instantiate. If
151 :attr:`~django.views.generic.edit.FormMixin.form_class` is provided,
152 that class will be used. Otherwise, a ``ModelForm`` will be
153 instantiated using the model associated with the
154 :attr:`~django.views.generic.detail.SingleObjectMixin.queryset`, or
155 with the :attr:`~django.views.generic.detail.SingleObjectMixin.model`,
156 depending on which attribute is provided.
157
158 .. method:: get_form_kwargs()
159
160 Add the current instance (``self.object``) to the standard
161 :meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
162
163 .. method:: get_success_url()
164
165 Determine the URL to redirect to when the form is successfully
166 validated. Returns
167 :attr:`django.views.generic.edit.ModelFormMixin.success_url` if it is
168 provided; otherwise, attempts to use the ``get_absolute_url()`` of the
169 object.
170
171 .. method:: form_valid(form)
172
173 Saves the form instance, sets the current object for the view, and
174 redirects to
175 :meth:`~django.views.generic.edit.FormMixin.get_success_url`.
176
177 .. method:: form_invalid(form)
178
179 Renders a response, providing the invalid form as context.
180
181
182 ``ProcessFormView``
183 ===================
184
185 .. class:: django.views.generic.edit.ProcessFormView
186
187 A mixin that provides basic HTTP GET and POST workflow.
188
189 .. note::
190
191 This is named 'ProcessFormView' and inherits directly from
192 :class:`django.views.generic.base.View`, but breaks if used
193 independently, so it is more of a mixin.
194
195 **Extends**
196
197 * :class:`django.views.generic.base.View`
198
199 **Methods and Attributes**
200
201 .. method:: get(request, *args, **kwargs)
202
203 Renders a response using a context created with
204 :meth:`~django.views.generic.edit.FormMixin.get_context_data`.
205
206 .. method:: post(request, *args, **kwargs)
207
208 Constructs a form, checks the form for validity, and handles it
209 accordingly.
210
211 .. method:: put(*args, **kwargs)
212
213 The ``PUT`` action is also handled and passes all parameters through to
214 :meth:`post`.
215
216
217 ``DeletionMixin``
218 =================
219
220 .. class:: django.views.generic.edit.DeletionMixin
221
222 Enables handling of the ``DELETE`` HTTP action.
223
224 **Methods and Attributes**
225
226 .. attribute:: success_url
227
228 The url to redirect to when the nominated object has been
229 successfully deleted.
230
231 ``success_url`` may contain dictionary string formatting, which will be
232 interpolated against the object's field attributes. For example, you
233 could use ``success_url="/parent/{parent_id}/"`` to redirect to a URL
234 composed out of the ``parent_id`` field on a model.
235
236 .. method:: delete(request, *args, **kwargs)
237
238 Retrieves the target object and calls its ``delete()`` method, then
239 redirects to the success URL.
240
241 .. method:: get_success_url()
242
243 Returns the url to redirect to when the nominated object has been
244 successfully deleted. Returns
245 :attr:`~django.views.generic.edit.DeletionMixin.success_url` by
246 default.