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