Generic Views
Django's generic class-based views are pre-built views that implement the most common web patterns — listing objects, showing one object's detail, and creating, updating, or deleting records — so you get full pages of working functionality by setting just a few attributes.
Learn Generic Views in our free Django course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Django course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll meet the five generic views you'll use every day — ListView, DetailView, CreateView, UpdateView, and DeleteView — and learn how to customise them when the defaults aren't quite enough.
ListView renders a page showing many objects, and DetailView renders a page for a single object. You point each at a model , and Django queries the database, passes the results to a template, and renders the response — all without you writing query or rendering code.
Here are the two views for a Post model in an app called blog :
If you leave out template_name and context_object_name , Django builds defaults from the app and model names:
The editing views build and process HTML forms for you. CreateView shows a blank form and saves a new record, UpdateView shows a form pre-filled with an existing record, and DeleteView shows a confirmation page before removing one.
For CreateView and UpdateView you list the editable fields , and you tell Django where to go after a successful save with success_url — or by defining get_absolute_url() on the model:
The defaults are great, but real pages need tweaks. Override get_queryset() to control which objects appear — filtering or ordering them — and override get_context_data() to add extra data to the template alongside the objects.
Always call super().get_context_data(**kwargs) first — that gives you the object list Django already prepared — then add your keys and return the dictionary.
Fill in the blank so the simulated view points at the right model. In real Django this is the model = Post attribute a generic view needs.
Django looked for the default template but couldn't find it on disk.
✅ Fix: create the file at blog/templates/blog/post_list.html , or set template_name to a template you do have.
❌ ImproperlyConfigured: No URL to redirect to
A CreateView or UpdateView saved successfully but doesn't know where to send the user next.
✅ Fix: set success_url on the view, or define get_absolute_url() on the model.
❌ AttributeError: 'Post' object has no attribute 'get_absolute_url'
The view tried to redirect to the object's URL, but the model never defined that method.
✅ Fix: add a get_absolute_url() method that returns a reverse(...) URL on the model.
Write a helper that produces the default template path Django would pick for a given app, model, and view suffix — for example blog/post_list.html .
Lesson 13 complete — you can build full pages with a few attributes!
You now know how ListView and DetailView display records, how CreateView, UpdateView, and DeleteView handle forms, what default templates and context names Django generates, and how to customise it all with get_queryset() and get_context_data().
🚀 Up next: Template Inheritance — share a common layout across pages with base templates and blocks.
Practice quiz
Which generic view displays a list of objects?
- DetailView
- ListView
- CreateView
- FormView
Answer: ListView. ListView fetches and renders a list of model instances.
Which generic view shows a single object's detail page?
- DetailView
- ListView
- UpdateView
- TemplateView
Answer: DetailView. DetailView fetches one object by its pk or slug and renders its detail template.
Which attribute tells a generic view which model to use?
- queryset_class
- table
- model
- object_name
Answer: model. You set the model attribute (or override get_queryset) so the view knows what to fetch.
Which generic view handles creating a new object via a form?
- DeleteView
- DetailView
- ListView
- CreateView
Answer: CreateView. CreateView renders a ModelForm and saves a new instance on valid POST.
What default template does a ListView for a Post model in app blog look for?
- blog/post_list.html
- blog/list.html
- post/list.html
- blog/posts.html
Answer: blog/post_list.html. ListView builds the default name app/model_list.html, i.e. blog/post_list.html.
Which method do you override to filter which objects a ListView shows?
- get_objects()
- get_queryset()
- filter_objects()
- get_list()
Answer: get_queryset(). Override get_queryset() and return the filtered or ordered queryset.
Which method adds extra data to a generic view's template context?
- add_context()
- extra_context_data()
- get_context_data()
- context()
Answer: get_context_data(). Override get_context_data(), call super(), add keys, and return the context.
Which template do CreateView and UpdateView share by default for a Post?
- blog/post_detail.html
- blog/post_list.html
- blog/post_edit.html
- blog/post_form.html
Answer: blog/post_form.html. Both use app/model_form.html, i.e. blog/post_form.html.
Which generic view confirms and deletes an object?
- DeleteView
- RemoveView
- DetailView
- ListView
Answer: DeleteView. DeleteView shows a confirmation page (model_confirm_delete.html) and deletes on POST.
When is a function view a better choice than a generic view?
- For a standard object list page
- For a basic create form
- When logic is unusual or doesn't map cleanly to one model
- Always
Answer: When logic is unusual or doesn't map cleanly to one model. Generic views shine for standard patterns; function views are clearer for unusual logic.