Generic editing views

The following views are described on this page and provide a foundation for editing content:

See also

The messages framework contains 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:

  1. from django.db import models
  2. from django.urls import reverse
  3. class Author(models.Model):
  4. name = models.CharField(max_length=200)
  5. def get_absolute_url(self):
  6. 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:

Example myapp/forms.py:

  1. from django import forms
  2. class ContactForm(forms.Form):
  3. name = forms.CharField()
  4. message = forms.CharField(widget=forms.Textarea)
  5. def send_email(self):
  6. # send email using the self.cleaned_data dictionary
  7. pass

Example myapp/views.py:

  1. from myapp.forms import ContactForm
  2. from django.views.generic.edit import FormView
  3. class ContactFormView(FormView):
  4. template_name = "contact.html"
  5. form_class = ContactForm
  6. success_url = "/thanks/"
  7. def form_valid(self, form):
  8. # This method is called when valid form data has been POSTed.
  9. # It should return an HttpResponse.
  10. form.send_email()
  11. return super().form_valid(form)

Example myapp/contact.html:

  1. <form method="post">{% csrf_token %}
  2. {{ form.as_p }}
  3. <input type="submit" value="Send message">
  4. </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 django.views.generic.edit.FormView or other views displaying a form.

Ancestors (MRO)

This view inherits methods and attributes from the following views:

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:

Attributes

  • 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'.

  • 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:

  1. from django.views.generic.edit import CreateView
  2. from myapp.models import Author
  3. class AuthorCreateView(CreateView):
  4. model = Author
  5. fields = ["name"]

Example myapp/author_form.html:

  1. <form method="post">{% csrf_token %}
  2. {{ form.as_p }}
  3. <input type="submit" value="Save">
  4. </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 django.views.generic.edit.CreateView.

Ancestors (MRO)

This view inherits methods and attributes from the following views:

Methods

  • get(request, *args, **kwargs)

    Sets the current object instance (self.object) to None.

  • 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:

Attributes

  • 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'.

  • object

    When using UpdateView you have access to self.object, which is the object being updated.

Example myapp/views.py:

  1. from django.views.generic.edit import UpdateView
  2. from myapp.models import Author
  3. class AuthorUpdateView(UpdateView):
  4. model = Author
  5. fields = ["name"]
  6. template_name_suffix = "_update_form"

Example myapp/author_update_form.html:

  1. <form method="post">{% csrf_token %}
  2. {{ form.as_p }}
  3. <input type="submit" value="Update">
  4. </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 django.views.generic.edit.UpdateView.

Ancestors (MRO)

This view inherits methods and attributes from the following views:

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. 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:

Attributes

  • form_class

    Inherited from BaseDeleteView. The form class that will be used to confirm the request. By default 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.

  • 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:

  1. from django.urls import reverse_lazy
  2. from django.views.generic.edit import DeleteView
  3. from myapp.models import Author
  4. class AuthorDeleteView(DeleteView):
  5. model = Author
  6. success_url = reverse_lazy("author-list")

Example myapp/author_confirm_delete.html:

  1. <form method="post">{% csrf_token %}
  2. <p>Are you sure you want to delete "{{ object }}"?</p>
  3. {{ form }}
  4. <input type="submit" value="Confirm">
  5. </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 django.views.generic.edit.DeleteView.

Ancestors (MRO)

This view inherits methods and attributes from the following views: