The following views are described on this page and provide a foundation forediting content:
- django.views.generic.edit.FormView
- django.views.generic.edit.CreateView
- django.views.generic.edit.UpdateView
- django.views.generic.edit.DeleteView
See also
The messages framework containsSuccessMessageMixin, whichfacilitates presenting messages about successful form submissions.
Note
Some of the examples on this page assume that an Author
model has beendefined as follows in myapp/models.py
:
from django.db import modelsfrom django.urls import reverseclass 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 validationerrors; on success, redirects to a new URL.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.base.TemplateResponseMixin
- django.views.generic.edit.BaseFormView
- django.views.generic.edit.FormMixin
- django.views.generic.edit.ProcessFormView
- django.views.generic.base.View
Example myapp/forms.py:
from django import formsclass 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 ContactFormfrom django.views.generic.edit import FormViewclass 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:
<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 thedjango.views.generic.edit.FormView or other views displaying aform.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.edit.FormMixin
- django.views.generic.edit.ProcessFormView
CreateView
¶
- class
django.views.generic.edit.
CreateView
¶ A view that displays a form for creating an object, redisplaying the formwith validation errors (if there are any) and saving the object.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.detail.SingleObjectTemplateResponseMixin
- django.views.generic.base.TemplateResponseMixin
- django.views.generic.edit.BaseCreateView
- django.views.generic.edit.ModelFormMixin
- django.views.generic.edit.FormMixin
- django.views.generic.detail.SingleObjectMixin
- django.views.generic.edit.ProcessFormView
- django.views.generic.base.View
Attributes
template_name_suffix
¶The
CreateView
page displayed to aGET
request uses atemplate_name_suffix
of'_form'
. Forexample, changing this attribute to'_create_form'
for a viewcreating objects for the exampleAuthor
model would cause thedefaulttemplate_name
to be'myapp/author_create_form.html'
.
object
¶When using
CreateView
you have access toself.object
, which isthe object being created. If the object hasn’t been created yet, thevalue will beNone
.
Example myapp/views.py:
from django.views.generic.edit import CreateViewfrom myapp.models import Authorclass AuthorCreateView(CreateView): model = Author fields = ["name"]
Example myapp/author_form.html:
<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 beused directly, but rather as a parent class of thedjango.views.generic.edit.CreateView.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.edit.ModelFormMixin
- django.views.generic.edit.ProcessFormView
Methods
get
(request, *args, **kwargs)¶Sets the current object instance (
self.object
) toNone
.
post
(request, *args, **kwargs)¶Sets the current object instance (
self.object
) toNone
.
UpdateView
¶
- class
django.views.generic.edit.
UpdateView
¶ A view that displays a form for editing an existing object, redisplayingthe form with validation errors (if there are any) and saving changes tothe object. This uses a form automatically generated from the object’smodel class (unless a form class is manually specified).
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.detail.SingleObjectTemplateResponseMixin
- django.views.generic.base.TemplateResponseMixin
- django.views.generic.edit.BaseUpdateView
- django.views.generic.edit.ModelFormMixin
- django.views.generic.edit.FormMixin
- django.views.generic.detail.SingleObjectMixin
- django.views.generic.edit.ProcessFormView
- django.views.generic.base.View
Attributes
template_name_suffix
¶The
UpdateView
page displayed to aGET
request uses atemplate_name_suffix
of'_form'
. Forexample, changing this attribute to'_update_form'
for a viewupdating objects for the exampleAuthor
model would cause thedefaulttemplate_name
to be'myapp/author_update_form.html'
.
object
¶When using
UpdateView
you have access toself.object
, which isthe object being updated.
Example myapp/views.py:
from django.views.generic.edit import UpdateViewfrom myapp.models import Authorclass AuthorUpdateView(UpdateView): model = Author fields = ["name"] template_name_suffix = "_update_form"
Example myapp/author_update_form.html:
<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 tobe used directly, but rather as a parent class of thedjango.views.generic.edit.UpdateView.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.edit.ModelFormMixin
- django.views.generic.edit.ProcessFormView
Methods
get
(request, *args, **kwargs)¶Sets the current object instance (
self.object
).
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
. Ifthis view is fetched viaGET
, it will display a confirmation page thatshould contain a form that POSTs to the same URL.Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.detail.SingleObjectTemplateResponseMixin
- django.views.generic.base.TemplateResponseMixin
- django.views.generic.edit.BaseDeleteView
- django.views.generic.edit.DeletionMixin
- django.views.generic.edit.FormMixin
- django.views.generic.base.ContextMixin
- django.views.generic.detail.BaseDetailView
- django.views.generic.detail.SingleObjectMixin
- django.views.generic.base.View
Attributes
form_class
¶Inherited from BaseDeleteView. Theform class that will be used to confirm the request. By defaultdjango.forms.Form, resulting in an empty form that is alwaysvalid.
By providing your own
Form
subclass, you can add additionalrequirements, such as a confirmation checkbox, for example.
template_name_suffix
¶The
DeleteView
page displayed to aGET
request uses atemplate_name_suffix
of'_confirm_delete'
. Forexample, changing this attribute to'_check_delete'
for a viewdeleting objects for the exampleAuthor
model would cause thedefaulttemplate_name
to be'myapp/author_check_delete.html'
.
Example myapp/views.py:
from django.urls import reverse_lazyfrom django.views.generic.edit import DeleteViewfrom myapp.models import Authorclass AuthorDeleteView(DeleteView): model = Author success_url = reverse_lazy("author-list")
Example myapp/author_confirm_delete.html:
<form method="post">{% csrf_token %} <p>Are you sure you want to delete "{{ object }}"?</p> {{ form }} <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 useddirectly, but rather as a parent class of thedjango.views.generic.edit.DeleteView.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.edit.DeletionMixin
- django.views.generic.edit.FormMixin
- django.views.generic.detail.BaseDetailView