2012-06-11 16:34:00 +08:00
|
|
|
=====================
|
|
|
|
Generic editing views
|
|
|
|
=====================
|
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
The following views are described on this page and provide a foundation for
|
2012-07-16 08:30:39 +08:00
|
|
|
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`
|
|
|
|
|
2012-08-05 04:01:40 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Some of the examples on this page assume that a model titled 'Author'
|
2012-08-11 14:07:15 +08:00
|
|
|
has been defined. For these cases we assume the following has been defined
|
2012-07-16 10:29:19 +08:00
|
|
|
in `myapp/models.py`::
|
2012-07-16 08:30:39 +08:00
|
|
|
|
|
|
|
from django.core.urlresolvers import reverse
|
2012-11-14 03:46:29 +08:00
|
|
|
from django.db import models
|
2012-07-16 08:30:39 +08:00
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=200)
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('author-detail', kwargs={'pk': self.pk})
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
FormView
|
|
|
|
--------
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
.. 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)**
|
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
* :class:`django.views.generic.edit.FormView`
|
|
|
|
* :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`
|
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
**Example 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 views.py**::
|
|
|
|
|
|
|
|
from myapp.forms import ContactForm
|
|
|
|
from django.views.generic.edit import FormView
|
|
|
|
|
|
|
|
class ContactView(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(ContactView, self).form_valid(form)
|
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
CreateView
|
|
|
|
----------
|
2012-07-16 08:30:39 +08:00
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
.. 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)**
|
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
* :class:`django.views.generic.edit.CreateView`
|
|
|
|
* :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`
|
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
**Attributes**
|
|
|
|
|
|
|
|
.. attribute:: template_name_suffix
|
|
|
|
|
|
|
|
The CreateView page displayed to a GET request uses a
|
2012-08-11 14:07:15 +08:00
|
|
|
``template_name_suffix`` of ``'_form.html'``. For
|
2012-08-05 04:01:40 +08:00
|
|
|
example, changing this attribute to ``'_create_form.html'`` for a view
|
|
|
|
creating objects for the the example `Author` model would cause the the
|
|
|
|
default `template_name` to be ``'myapp/author_create_form.html'``.
|
2012-07-16 08:30:39 +08:00
|
|
|
|
|
|
|
**Example views.py**::
|
|
|
|
|
|
|
|
from django.views.generic.edit import CreateView
|
|
|
|
from myapp.models import Author
|
|
|
|
|
|
|
|
class AuthorCreate(CreateView):
|
|
|
|
model = Author
|
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
UpdateView
|
|
|
|
----------
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
.. 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)**
|
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
* :class:`django.views.generic.edit.UpdateView`
|
|
|
|
* :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`
|
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
**Attributes**
|
|
|
|
|
|
|
|
.. attribute:: template_name_suffix
|
|
|
|
|
2012-08-05 04:01:40 +08:00
|
|
|
The UpdateView page displayed to a GET request uses a
|
2012-08-11 14:07:15 +08:00
|
|
|
``template_name_suffix`` of ``'_form.html'``. For
|
2012-08-05 04:01:40 +08:00
|
|
|
example, changing this attribute to ``'_update_form.html'`` for a view
|
|
|
|
updating objects for the the example `Author` model would cause the the
|
2012-08-11 14:07:15 +08:00
|
|
|
default `template_name` to be ``'myapp/author_update_form.html'``.
|
2012-07-16 08:30:39 +08:00
|
|
|
|
|
|
|
**Example views.py**::
|
|
|
|
|
|
|
|
from django.views.generic.edit import UpdateView
|
|
|
|
from myapp.models import Author
|
|
|
|
|
|
|
|
class AuthorUpdate(UpdateView):
|
|
|
|
model = Author
|
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
DeleteView
|
|
|
|
----------
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
.. 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)**
|
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
* :class:`django.views.generic.edit.DeleteView`
|
|
|
|
* :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`
|
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
**Attributes**
|
|
|
|
|
|
|
|
.. attribute:: template_name_suffix
|
|
|
|
|
2012-08-05 04:01:40 +08:00
|
|
|
The DeleteView page displayed to a GET request uses a
|
2012-08-11 14:07:15 +08:00
|
|
|
``template_name_suffix`` of ``'_confirm_delete.html'``. For
|
2012-08-05 04:01:40 +08:00
|
|
|
example, changing this attribute to ``'_check_delete.html'`` for a view
|
|
|
|
deleting objects for the the example `Author` model would cause the the
|
2012-08-11 14:07:15 +08:00
|
|
|
default `template_name` to be ``'myapp/author_check_delete.html'``.
|
2012-08-05 04:01:40 +08:00
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
|
|
|
|
**Example views.py**::
|
|
|
|
|
|
|
|
from django.views.generic.edit import DeleteView
|
|
|
|
from django.core.urlresolvers import reverse_lazy
|
|
|
|
from myapp.models import Author
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-07-16 08:30:39 +08:00
|
|
|
class AuthorDelete(DeleteView):
|
|
|
|
model = Author
|
2012-08-11 14:07:15 +08:00
|
|
|
success_url = reverse_lazy('author-list')
|