Django Editing Views: CreateView, UpdateView & DeleteView

Building CRUD (Create, Read, Update, Delete) operations is the core requirement of database-backed web development. Django provides three specialized generic editing views that handle form rendering, validation cycles, database persistence, and redirection: CreateView, UpdateView, and DeleteView.

1. CreateView: Automatic Form Generation & Author Injection

# blog/views.py
from django.views.generic.edit import CreateView
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import BlogPost

class BlogPostCreateView(LoginRequiredMixin, CreateView):
    model = BlogPost
    fields = ['title', 'summary', 'content', 'category', 'is_published']
    template_name = 'blog/post_form.html'
    success_url = reverse_lazy('blog:post_list')

    def form_valid(self, form):
        """Automatically bind logged-in author before saving record."""
        form.instance.author = self.request.user.profile
        return super().form_valid(form)

2. UpdateView: Modifying Existing Records

from django.views.generic.edit import UpdateView
from django.core.exceptions import PermissionDenied

class BlogPostUpdateView(LoginRequiredMixin, UpdateView):
    model = BlogPost
    fields = ['title', 'summary', 'content', 'category', 'is_published']
    template_name = 'blog/post_form.html'
    slug_field = 'slug'

    def get_object(self, queryset=None):
        """Ensure users can only edit their own authored articles."""
        obj = super().get_object(queryset)
        if obj.author.user != self.request.user:
            raise PermissionDenied("You do not have permission to edit this post.")
        return obj

3. DeleteView: Safe Confirmation & Removal

from django.views.generic.edit import DeleteView

class BlogPostDeleteView(LoginRequiredMixin, DeleteView):
    model = BlogPost
    template_name = 'blog/post_confirm_delete.html'
    success_url = reverse_lazy('blog:post_list')
    slug_field = 'slug'

reverse vs reverse_lazy

When setting success_url on a class level in CBVs, always use reverse_lazy() instead of reverse()! Because Python evaluates class attributes when the module is imported — before URLconf has finished loading — standard reverse() will raise a NoReverseMatch error.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is the function of CreateView in Django?

CreateView displays an HTML form for creating a new database record, handles validation on POST submission, creates and saves the new model instance, and redirects the user to the configured `success_url`.

Q2. How does UpdateView know which record to edit?

UpdateView inspects the URL parameters (typically `` or ``) to retrieve the existing model instance from the database, prepopulates the HTML form with current values, and saves changes on submission.

Q3. Why is DeleteView safer than deleting records via GET requests?

HTTP GET requests should be idempotent and never cause destructive state modifications. DeleteView requires an HTTP POST request to confirm deletion, preventing accidental record deletions caused by browser pre-fetching or web crawler links.

Q4. What does overriding form_valid() allow you to do?

`form_valid()` runs after form data passes all validation rules but before the instance is saved. It is the ideal place to attach system-assigned attributes, such as assigning `form.instance.author = self.request.user`.

Explore Related Django Tutorials & Next Steps

Broaden your backend architecture, REST API design, and production engineering expertise:

Generic Class-Based Views (CBVs)

Core inheritance hierarchy underpinning all editing views.

ModelForms & Custom Validation

Customize the form_class used by CreateView and UpdateView.

HTTP Redirects & get_absolute_url

Configure success_url or get_absolute_url() after form submissions.

15+ Django Practice Coding Exercises

Build an interactive CRUD application using editing class-based views.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!