Generic editing views

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

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:

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

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

Example myapp/views.py:

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

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:

Example myapp/views.py:

  1. from django.views.generic.edit import CreateView
  2. from myapp.models import Author
  3.  
  4. class AuthorCreate(CreateView):
  5. model = Author
  6. 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>

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:

Example myapp/views.py:

  1. from django.views.generic.edit import UpdateView
  2. from myapp.models import Author
  3.  
  4. class AuthorUpdate(UpdateView):
  5. model = Author
  6. fields = ['name']
  7. 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>

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 via GET, 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:

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.  
  5. class AuthorDelete(DeleteView):
  6. model = Author
  7. 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. <input type="submit" value="Confirm">
  4. </form>