Debian Codegraph @

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

raw
1 =====================
2 Generic editing views
3 =====================
4
5 The following views are described on this page and provide a foundation for
6 editing content:
7
8 * :class:`django.views.generic.edit.FormView`
9 * :class:`django.views.generic.edit.CreateView`
10 * :class:`django.views.generic.edit.UpdateView`
11 * :class:`django.views.generic.edit.DeleteView`
12
13 .. seealso::
14
15 The :doc:`messages framework </ref/contrib/messages>` contains
16 :class:`~django.contrib.messages.views.SuccessMessageMixin`, which
17 facilitates presenting messages about successful form submissions.
18
19 .. note::
20
21 Some of the examples on this page assume that an ``Author`` model has been
22 defined as follows in ``myapp/models.py``::
23
24 from django.db import models
25 from django.urls import reverse
26
27
28 class Author(models.Model):
29 name = models.CharField(max_length=200)
30
31 def get_absolute_url(self):
32 return reverse("author-detail", kwargs={"pk": self.pk})
33
34 ``FormView``
35 ============
36
37 .. class:: django.views.generic.edit.FormView
38
39 A view that displays a form. On error, redisplays the form with validation
40 errors; on success, redirects to a new URL.
41
42 **Ancestors (MRO)**
43
44 This view inherits methods and attributes from the following views:
45
46 * :class:`django.views.generic.base.TemplateResponseMixin`
47 * :class:`django.views.generic.edit.BaseFormView`
48 * :class:`django.views.generic.edit.FormMixin`
49 * :class:`django.views.generic.edit.ProcessFormView`
50 * :class:`django.views.generic.base.View`
51
52 **Example myapp/forms.py**::
53
54 from django import forms
55
56
57 class ContactForm(forms.Form):
58 name = forms.CharField()
59 message = forms.CharField(widget=forms.Textarea)
60
61 def send_email(self):
62 # send email using the self.cleaned_data dictionary
63 pass
64
65 **Example myapp/views.py**::
66
67 from myapp.forms import ContactForm
68 from django.views.generic.edit import FormView
69
70
71 class ContactFormView(FormView):
72 template_name = "contact.html"
73 form_class = ContactForm
74 success_url = "/thanks/"
75
76 def form_valid(self, form):
77 # This method is called when valid form data has been POSTed.
78 # It should return an HttpResponse.
79 form.send_email()
80 return super().form_valid(form)
81
82 **Example myapp/contact.html**:
83
84 .. code-block:: html+django
85
86 <form method="post">{% csrf_token %}
87 {{ form.as_p }}
88 <input type="submit" value="Send message">
89 </form>
90
91 .. class:: django.views.generic.edit.BaseFormView
92
93 A base view for displaying a form. It is not intended to be used directly,
94 but rather as a parent class of the
95 :class:`django.views.generic.edit.FormView` or other views displaying a
96 form.
97
98 **Ancestors (MRO)**
99
100 This view inherits methods and attributes from the following views:
101
102 * :class:`django.views.generic.edit.FormMixin`
103 * :class:`django.views.generic.edit.ProcessFormView`
104
105 ``CreateView``
106 ==============
107
108 .. class:: django.views.generic.edit.CreateView
109
110 A view that displays a form for creating an object, redisplaying the form
111 with validation errors (if there are any) and saving the object.
112
113 **Ancestors (MRO)**
114
115 This view inherits methods and attributes from the following views:
116
117 * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
118 * :class:`django.views.generic.base.TemplateResponseMixin`
119 * :class:`django.views.generic.edit.BaseCreateView`
120 * :class:`django.views.generic.edit.ModelFormMixin`
121 * :class:`django.views.generic.edit.FormMixin`
122 * :class:`django.views.generic.detail.SingleObjectMixin`
123 * :class:`django.views.generic.edit.ProcessFormView`
124 * :class:`django.views.generic.base.View`
125
126 **Attributes**
127
128 .. attribute:: template_name_suffix
129
130 The ``CreateView`` page displayed to a ``GET`` request uses a
131 ``template_name_suffix`` of ``'_form'``. For
132 example, changing this attribute to ``'_create_form'`` for a view
133 creating objects for the example ``Author`` model would cause the
134 default ``template_name`` to be ``'myapp/author_create_form.html'``.
135
136 .. attribute:: object
137
138 When using ``CreateView`` you have access to ``self.object``, which is
139 the object being created. If the object hasn't been created yet, the
140 value will be ``None``.
141
142 **Example myapp/views.py**::
143
144 from django.views.generic.edit import CreateView
145 from myapp.models import Author
146
147
148 class AuthorCreateView(CreateView):
149 model = Author
150 fields = ["name"]
151
152 **Example myapp/author_form.html**:
153
154 .. code-block:: html+django
155
156 <form method="post">{% csrf_token %}
157 {{ form.as_p }}
158 <input type="submit" value="Save">
159 </form>
160
161 .. class:: django.views.generic.edit.BaseCreateView
162
163 A base view for creating a new object instance. It is not intended to be
164 used directly, but rather as a parent class of the
165 :class:`django.views.generic.edit.CreateView`.
166
167 **Ancestors (MRO)**
168
169 This view inherits methods and attributes from the following views:
170
171 * :class:`django.views.generic.edit.ModelFormMixin`
172 * :class:`django.views.generic.edit.ProcessFormView`
173
174 **Methods**
175
176 .. method:: get(request, *args, **kwargs)
177
178 Sets the current object instance (``self.object``) to ``None``.
179
180 .. method:: post(request, *args, **kwargs)
181
182 Sets the current object instance (``self.object``) to ``None``.
183
184 ``UpdateView``
185 ==============
186
187 .. class:: django.views.generic.edit.UpdateView
188
189 A view that displays a form for editing an existing object, redisplaying
190 the form with validation errors (if there are any) and saving changes to
191 the object. This uses a form automatically generated from the object's
192 model class (unless a form class is manually specified).
193
194 **Ancestors (MRO)**
195
196 This view inherits methods and attributes from the following views:
197
198 * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
199 * :class:`django.views.generic.base.TemplateResponseMixin`
200 * :class:`django.views.generic.edit.BaseUpdateView`
201 * :class:`django.views.generic.edit.ModelFormMixin`
202 * :class:`django.views.generic.edit.FormMixin`
203 * :class:`django.views.generic.detail.SingleObjectMixin`
204 * :class:`django.views.generic.edit.ProcessFormView`
205 * :class:`django.views.generic.base.View`
206
207 **Attributes**
208
209 .. attribute:: template_name_suffix
210
211 The ``UpdateView`` page displayed to a ``GET`` request uses a
212 ``template_name_suffix`` of ``'_form'``. For
213 example, changing this attribute to ``'_update_form'`` for a view
214 updating objects for the example ``Author`` model would cause the
215 default ``template_name`` to be ``'myapp/author_update_form.html'``.
216
217 .. attribute:: object
218
219 When using ``UpdateView`` you have access to ``self.object``, which is
220 the object being updated.
221
222 **Example myapp/views.py**::
223
224 from django.views.generic.edit import UpdateView
225 from myapp.models import Author
226
227
228 class AuthorUpdateView(UpdateView):
229 model = Author
230 fields = ["name"]
231 template_name_suffix = "_update_form"
232
233 **Example myapp/author_update_form.html**:
234
235 .. code-block:: html+django
236
237 <form method="post">{% csrf_token %}
238 {{ form.as_p }}
239 <input type="submit" value="Update">
240 </form>
241
242 .. class:: django.views.generic.edit.BaseUpdateView
243
244 A base view for updating an existing object instance. It is not intended to
245 be used directly, but rather as a parent class of the
246 :class:`django.views.generic.edit.UpdateView`.
247
248 **Ancestors (MRO)**
249
250 This view inherits methods and attributes from the following views:
251
252 * :class:`django.views.generic.edit.ModelFormMixin`
253 * :class:`django.views.generic.edit.ProcessFormView`
254
255 **Methods**
256
257 .. method:: get(request, *args, **kwargs)
258
259 Sets the current object instance (``self.object``).
260
261 .. method:: post(request, *args, **kwargs)
262
263 Sets the current object instance (``self.object``).
264
265 ``DeleteView``
266 ==============
267
268 .. class:: django.views.generic.edit.DeleteView
269
270 A view that displays a confirmation page and deletes an existing object.
271 The given object will only be deleted if the request method is ``POST``. If
272 this view is fetched via ``GET``, it will display a confirmation page that
273 should contain a form that POSTs to the same URL.
274
275 **Ancestors (MRO)**
276
277 This view inherits methods and attributes from the following views:
278
279 * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
280 * :class:`django.views.generic.base.TemplateResponseMixin`
281 * :class:`django.views.generic.edit.BaseDeleteView`
282 * :class:`django.views.generic.edit.DeletionMixin`
283 * :class:`django.views.generic.edit.FormMixin`
284 * :class:`django.views.generic.base.ContextMixin`
285 * :class:`django.views.generic.detail.BaseDetailView`
286 * :class:`django.views.generic.detail.SingleObjectMixin`
287 * :class:`django.views.generic.base.View`
288
289 **Attributes**
290
291 .. attribute:: form_class
292
293 Inherited from :class:`~django.views.generic.edit.BaseDeleteView`. The
294 form class that will be used to confirm the request. By default
295 :class:`django.forms.Form`, resulting in an empty form that is always
296 valid.
297
298 By providing your own ``Form`` subclass, you can add additional
299 requirements, such as a confirmation checkbox, for example.
300
301 .. attribute:: template_name_suffix
302
303 The ``DeleteView`` page displayed to a ``GET`` request uses a
304 ``template_name_suffix`` of ``'_confirm_delete'``. For
305 example, changing this attribute to ``'_check_delete'`` for a view
306 deleting objects for the example ``Author`` model would cause the
307 default ``template_name`` to be ``'myapp/author_check_delete.html'``.
308
309 **Example myapp/views.py**::
310
311 from django.urls import reverse_lazy
312 from django.views.generic.edit import DeleteView
313 from myapp.models import Author
314
315
316 class AuthorDeleteView(DeleteView):
317 model = Author
318 success_url = reverse_lazy("author-list")
319
320 **Example myapp/author_confirm_delete.html**:
321
322 .. code-block:: html+django
323
324 <form method="post">{% csrf_token %}
325 <p>Are you sure you want to delete "{{ object }}"?</p>
326 {{ form }}
327 <input type="submit" value="Confirm">
328 </form>
329
330 .. class:: django.views.generic.edit.BaseDeleteView
331
332 A base view for deleting an object instance. It is not intended to be used
333 directly, but rather as a parent class of the
334 :class:`django.views.generic.edit.DeleteView`.
335
336 **Ancestors (MRO)**
337
338 This view inherits methods and attributes from the following views:
339
340 * :class:`django.views.generic.edit.DeletionMixin`
341 * :class:`django.views.generic.edit.FormMixin`
342 * :class:`django.views.generic.detail.BaseDetailView`