2012-06-11 08:34:00 +00:00
|
|
|
=====================
|
|
|
|
Generic editing views
|
|
|
|
=====================
|
|
|
|
|
2012-08-11 06:07:15 +00:00
|
|
|
The following views are described on this page and provide a foundation for
|
2012-07-16 00:30:39 +00: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`
|
|
|
|
|
2018-05-24 07:18:20 +00:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
The :doc:`messages framework </ref/contrib/messages>` contains
|
|
|
|
:class:`~django.contrib.messages.views.SuccessMessageMixin`, which
|
|
|
|
facilitates presenting messages about successful form submissions.
|
|
|
|
|
2012-08-04 20:01:40 +00:00
|
|
|
.. note::
|
|
|
|
|
2013-04-15 20:23:35 +00:00
|
|
|
Some of the examples on this page assume that an ``Author`` model has been
|
2012-11-28 00:26:37 +00:00
|
|
|
defined as follows in ``myapp/models.py``::
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2012-11-13 19:46:29 +00:00
|
|
|
from django.db import models
|
2018-05-12 17:37:42 +00:00
|
|
|
from django.urls import reverse
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2012-07-16 00:30:39 +00:00
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=200)
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2023-02-28 19:53:28 +00:00
|
|
|
return reverse("author-detail", kwargs={"pk": self.pk})
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``FormView``
|
|
|
|
============
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00: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 00:30:39 +00:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
2021-02-08 11:01:01 +00:00
|
|
|
* :class:`django.views.generic.edit.BaseFormView`
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.edit.FormMixin`
|
|
|
|
* :class:`django.views.generic.edit.ProcessFormView`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/forms.py**::
|
2012-07-16 00:30:39 +00:00
|
|
|
|
|
|
|
from django import forms
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2012-07-16 00:30:39 +00:00
|
|
|
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
|
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/views.py**::
|
2012-07-16 00:30:39 +00:00
|
|
|
|
|
|
|
from myapp.forms import ContactForm
|
|
|
|
from django.views.generic.edit import FormView
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
class ContactFormView(FormView):
|
2023-02-28 19:53:28 +00:00
|
|
|
template_name = "contact.html"
|
2012-07-16 00:30:39 +00:00
|
|
|
form_class = ContactForm
|
2023-02-28 19:53:28 +00:00
|
|
|
success_url = "/thanks/"
|
2012-07-16 00:30:39 +00:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
# This method is called when valid form data has been POSTed.
|
|
|
|
# It should return an HttpResponse.
|
|
|
|
form.send_email()
|
2017-01-22 06:57:14 +00:00
|
|
|
return super().form_valid(form)
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/contact.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2018-05-02 13:20:04 +00:00
|
|
|
<form method="post">{% csrf_token %}
|
2013-06-11 20:32:39 +00:00
|
|
|
{{ form.as_p }}
|
2018-01-21 07:09:10 +00:00
|
|
|
<input type="submit" value="Send message">
|
2013-06-11 20:32:39 +00:00
|
|
|
</form>
|
|
|
|
|
2021-02-08 11:01:01 +00:00
|
|
|
.. 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`
|
2013-06-11 20:32:39 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``CreateView``
|
|
|
|
==============
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2012-06-11 08:34:00 +00: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 00:30:39 +00:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
2021-02-08 11:01:01 +00:00
|
|
|
* :class:`django.views.generic.edit.BaseCreateView`
|
2012-06-11 08:34:00 +00:00
|
|
|
* :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 00:30:39 +00:00
|
|
|
**Attributes**
|
|
|
|
|
|
|
|
.. attribute:: template_name_suffix
|
|
|
|
|
2013-01-01 23:45:57 +00:00
|
|
|
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'``.
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2014-02-09 10:50:09 +00:00
|
|
|
.. 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``.
|
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/views.py**::
|
2012-07-16 00:30:39 +00:00
|
|
|
|
|
|
|
from django.views.generic.edit import CreateView
|
|
|
|
from myapp.models import Author
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
class AuthorCreateView(CreateView):
|
2012-07-16 00:30:39 +00:00
|
|
|
model = Author
|
2023-02-28 19:53:28 +00:00
|
|
|
fields = ["name"]
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/author_form.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2018-05-02 13:20:04 +00:00
|
|
|
<form method="post">{% csrf_token %}
|
2013-06-11 20:32:39 +00:00
|
|
|
{{ form.as_p }}
|
2018-01-21 07:09:10 +00:00
|
|
|
<input type="submit" value="Save">
|
2013-06-11 20:32:39 +00:00
|
|
|
</form>
|
|
|
|
|
2021-02-08 11:01:01 +00:00
|
|
|
.. 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``.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``UpdateView``
|
|
|
|
==============
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00: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 00:30:39 +00:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
2021-02-08 11:01:01 +00:00
|
|
|
* :class:`django.views.generic.edit.BaseUpdateView`
|
2012-06-11 08:34:00 +00:00
|
|
|
* :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 00:30:39 +00:00
|
|
|
**Attributes**
|
|
|
|
|
|
|
|
.. attribute:: template_name_suffix
|
|
|
|
|
2013-01-01 23:45:57 +00:00
|
|
|
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'``.
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2014-02-09 10:50:09 +00:00
|
|
|
.. attribute:: object
|
|
|
|
|
|
|
|
When using ``UpdateView`` you have access to ``self.object``, which is
|
|
|
|
the object being updated.
|
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/views.py**::
|
2012-07-16 00:30:39 +00:00
|
|
|
|
|
|
|
from django.views.generic.edit import UpdateView
|
|
|
|
from myapp.models import Author
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
class AuthorUpdateView(UpdateView):
|
2012-07-16 00:30:39 +00:00
|
|
|
model = Author
|
2023-02-28 19:53:28 +00:00
|
|
|
fields = ["name"]
|
|
|
|
template_name_suffix = "_update_form"
|
2013-06-11 20:32:39 +00:00
|
|
|
|
|
|
|
**Example myapp/author_update_form.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2018-05-02 13:20:04 +00:00
|
|
|
<form method="post">{% csrf_token %}
|
2013-06-11 20:32:39 +00:00
|
|
|
{{ form.as_p }}
|
2018-01-21 07:09:10 +00:00
|
|
|
<input type="submit" value="Update">
|
2013-06-11 20:32:39 +00:00
|
|
|
</form>
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2021-02-08 11:01:01 +00:00
|
|
|
.. 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``).
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``DeleteView``
|
|
|
|
==============
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00: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 00:30:39 +00:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
2021-02-08 11:01:01 +00:00
|
|
|
* :class:`django.views.generic.edit.BaseDeleteView`
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.edit.DeletionMixin`
|
2021-07-13 14:06:12 +00:00
|
|
|
* :class:`django.views.generic.edit.FormMixin`
|
|
|
|
* :class:`django.views.generic.base.ContextMixin`
|
2021-02-03 14:25:56 +00:00
|
|
|
* :class:`django.views.generic.detail.BaseDetailView`
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.detail.SingleObjectMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
2012-07-16 00:30:39 +00:00
|
|
|
**Attributes**
|
|
|
|
|
2021-07-13 14:06:12 +00:00
|
|
|
.. attribute:: form_class
|
|
|
|
|
|
|
|
Inherited from :class:`~django.views.generic.edit.BaseDeleteView`. The
|
|
|
|
form class that will be used to confirm the request. By default
|
|
|
|
:class:`django.forms.Form`, resulting in an empty form that is always
|
|
|
|
valid.
|
|
|
|
|
|
|
|
By providing your own ``Form`` subclass, you can add additional
|
|
|
|
requirements, such as a confirmation checkbox, for example.
|
|
|
|
|
2012-07-16 00:30:39 +00:00
|
|
|
.. attribute:: template_name_suffix
|
|
|
|
|
2013-01-01 23:45:57 +00:00
|
|
|
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'``.
|
2012-08-04 20:01:40 +00:00
|
|
|
|
2013-06-11 20:32:39 +00:00
|
|
|
**Example myapp/views.py**::
|
2012-07-16 00:30:39 +00:00
|
|
|
|
2015-12-30 15:51:16 +00:00
|
|
|
from django.urls import reverse_lazy
|
2018-05-12 17:37:42 +00:00
|
|
|
from django.views.generic.edit import DeleteView
|
2012-07-16 00:30:39 +00:00
|
|
|
from myapp.models import Author
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
class AuthorDeleteView(DeleteView):
|
2012-07-16 00:30:39 +00:00
|
|
|
model = Author
|
2023-02-28 19:53:28 +00:00
|
|
|
success_url = reverse_lazy("author-list")
|
2013-06-11 20:32:39 +00:00
|
|
|
|
|
|
|
**Example myapp/author_confirm_delete.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2018-05-02 13:20:04 +00:00
|
|
|
<form method="post">{% csrf_token %}
|
2013-06-11 20:32:39 +00:00
|
|
|
<p>Are you sure you want to delete "{{ object }}"?</p>
|
2021-07-13 14:06:12 +00:00
|
|
|
{{ form }}
|
2018-01-21 07:09:10 +00:00
|
|
|
<input type="submit" value="Confirm">
|
2013-06-11 20:32:39 +00:00
|
|
|
</form>
|
2021-02-08 11:01:01 +00:00
|
|
|
|
|
|
|
.. 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`
|
2021-07-13 14:06:12 +00:00
|
|
|
* :class:`django.views.generic.edit.FormMixin`
|
2021-02-08 11:01:01 +00:00
|
|
|
* :class:`django.views.generic.detail.BaseDetailView`
|