← Back to Django Course | Chapter 9: Class-Based Views | Lesson 5 of 10

CreateView Basics

CreateView is a ready-made form page that lets you add a brand-new row to the database without writing the save logic yourself.

A Minimal CreateView

Setting model and fields on a CreateView subclass is enough for Django to build a ModelForm automatically and save a new row when it is submitted and valid.

Example: A Minimal CreateView

BookCreateView auto-generates a form for title, author, and published_date, then redirects to book-list on success.

markup
from django.views.generic.edit import CreateView
from django.urls import reverse_lazy
from .models import Book


class BookCreateView(CreateView):
    model = Book
    fields = ['title', 'author', 'published_date']
    success_url = reverse_lazy('book-list')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Rendering the Create Form

CreateView passes a form object into the template context, which is rendered the same way as any other Django form, including the required CSRF token.

Warning: A CreateView template must POST back to the same URL, so leave the <form> action empty or point it at itself.

Example: Rendering the Create Form

The form variable is the auto-built ModelForm; {% csrf_token %} is still required for the POST to succeed.

markup
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Save Book</button>
</form>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Redirecting After a Successful Save

success_url tells CreateView where to send the browser after the new object is saved. reverse_lazy is used instead of reverse because the URL is evaluated when the class is defined, before the URLconf is fully loaded.

Example: Redirecting After a Successful Save

reverse_lazy("book-list") resolves the book-list URL only when it is actually needed, avoiding import-time errors.

markup
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
from .models import Book


class BookCreateView(CreateView):
    model = Book
    fields = ['title', 'author']
    success_url = reverse_lazy('book-list')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Forgetting to set fields (or exclude) on a CreateView, which raises an error because Django does not know which fields to expose.
  2. Not setting success_url or get_absolute_url on the model, so Django cannot redirect after a successful save.
  3. Assuming CreateView automatically requires login — it does not, unless combined with LoginRequiredMixin.
Chapter Summary
  • CreateView generates a form from a model and saves a new object when that form is valid.
  • fields (or exclude) tells CreateView which model fields to expose as form inputs.
  • success_url controls where the browser redirects after a successful creation.
  • CreateView still requires {% csrf_token %} in its template just like a manual form.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.