UpdateView Basics
A Minimal UpdateView
UpdateView needs model, fields, and a URL pattern with pk so it can load the existing object, pre-fill the form, and save changes back to that same row.
Example: A Minimal UpdateView
BookUpdateView loads the Book matching the URL's pk and pre-fills the form with its current field values.
from django.views.generic.edit import UpdateView
from django.urls import reverse_lazy
from .models import Book
class BookUpdateView(UpdateView):
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. #}
Wiring the Edit URL
The URL pattern must capture pk so Django knows exactly which object to load before rendering the pre-filled edit form.
Example: Wiring the Edit URL
The <int:pk> segment lets UpdateView run Book.objects.get(pk=pk) before showing the form.
from django.urls import path
from .views import BookUpdateView
urlpatterns = [
path('books/<int:pk>/edit/', BookUpdateView.as_view(), name='book-edit'),
]
{# 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. #}
A Dynamic Redirect with get_success_url
Overriding get_success_url() instead of setting a static success_url lets the redirect depend on the specific object that was just updated, such as sending the user to that object's own detail page.
Example: A Dynamic Redirect with get_success_url
self.object is the just-saved Book instance, so the redirect goes straight to that book's detail page.
from django.urls import reverse
from django.views.generic.edit import UpdateView
from .models import Book
class BookUpdateView(UpdateView):
model = Book
fields = ['title', 'author']
def get_success_url(self):
return reverse('book-detail', kwargs={'pk': self.object.pk})
{# 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. #}
- Forgetting the URL needs a pk (or slug) so UpdateView knows which existing object to edit.
- Setting fields to a different list than CreateView's and being confused when some fields silently do not show up.
- Not overriding get_success_url() when the redirect target depends on the specific object just edited.
- UpdateView loads an existing object (via pk or slug from the URL) and pre-fills a ModelForm with its current values.
- fields controls which model fields are editable, just like CreateView.
- get_success_url() can be overridden for a dynamic, per-object redirect after saving.
- The template looks and behaves like a CreateView template, but the form starts pre-populated.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: