0Pricing
Django Academy · Lesson

CreateView and UpdateView

Build add and edit pages from a model.

CreateView and UpdateView is a free Django Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Pages That Write Data

Showing data is half the job. To add and edit records you reach for CreateView and UpdateView.

Meet CreateView

CreateView builds an add page: it renders a form, validates input, and saves a new record for you.

A Form from Fields

Tell CreateView the fields you want, and it generates a matching form straight from your model.

from django.views.generic import CreateView

class PostCreate(CreateView):
    model = Post
    fields = ["title", "body"]

The Form Template Name

CreateView and UpdateView both look for app/model_form.html, like post_form.html, by default.

Rendering the Form

Inside the template the generated form arrives as form, so you render its fields and a submit button.

<form method="post">{% csrf_token %}
  {{ form.as_p }}
  <button>Save</button>
</form>

Meet UpdateView

UpdateView works just like CreateView, but it loads an existing record first so the form starts pre-filled.

Editing an Existing Record

UpdateView reads a pk from the URL, fetches that object, and saves your changes back to it.

from django.views.generic import UpdateView

class PostUpdate(UpdateView):
    model = Post
    fields = ["title", "body"]

Where to Go After Saving

After a successful save, the view redirects. Set success_url to control exactly where users land next.

from django.urls import reverse_lazy

class PostCreate(CreateView):
    success_url = reverse_lazy("post_list")

Or Let the Model Decide

Skip success_url by giving your model a get_absolute_url; the view will redirect there automatically.

def get_absolute_url(self):
    return reverse("post_detail", args=[self.pk])

Wiring the Edit URLs

Create needs no pk, but update does, so its URL captures one with as_view() like the others.

path("new/", PostCreate.as_view()),
path("<int:pk>/edit/", PostUpdate.as_view()),

Shared Form Power

Because both reuse the same form and template, add and edit pages stay consistent with almost no extra code. 💪

Quick Check

What is the main difference between CreateView and UpdateView?

Recap: Create and Update

You used CreateView to add records and UpdateView to edit them, shared one form template, and steered redirects with success_url. 🎉

Frequently asked questions

Is the “CreateView and UpdateView” lesson free?

Yes — the full text of “CreateView and UpdateView” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “CreateView and UpdateView”?

Build add and edit pages from a model. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “CreateView and UpdateView” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. ListView and DetailView
  2. CreateView and UpdateView
  3. DeleteView and success_url
  4. Overriding get_queryset and Templates
← Back to Django Academy