Django class-based views: as_view and dispatch
Answer: Class-based views let you implement a view as a Python object rather than as a function, without replacing function-based views. as_view is a class method that returns a callable for the URL pattern. dispatch inspects whether the request is GET or POST and then relays it to a matching method.
Examples
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path("about/", TemplateView.as_view(template_name="about.html")),
]
Notes
TemplateView uses the dispatch behavior defined in the View base class. Mixins (multiple inheritance) can be used to factor code into reusable components. Code related to specific HTTP methods can be addressed by separate methods, not by conditional branching.
Reference list
Class-basedviews implement views as Python objects instead of functions.Mixinscan factor code into reusable components.Class-basedviews are an alternative to function-based views.
See also
Claims
- id: C1 claim: Class-based views implement views as Python objects instead of functions. source: S1 evidence: "They do not replace function-based views, but"
- id: C2 claim: Mixins can factor code into reusable components. source: S2 evidence: "used to factor code into reusable components"
- id: C3 claim: Class-based views are an alternative to function-based views. source: S2 evidence: "Class-based views provide an alternative way to implement views as Python"
Sources
- [S1] Introduction to class-based views. Django docs. https://docs.djangoproject.com/en/5.2/topics/class-based-views/intro/ . License: BSD-3-Clause. Accessed 2026-11-18.
- [S2] intro.txt. Django. https://raw.githubusercontent.com/django/django/stable/5.2.x/docs/topics/class-based-views/intro.txt . License: BSD-3-Clause. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Django docs, Introduction to class-based views; Django, intro.txt. BSD-3-Clause (https://raw.githubusercontent.com/django/django/main/LICENSE). Copyright Django Software Foundation and individual contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, Introduction to class-based views; Django, intro.txt. BSD-3-Clause (https://raw.githubusercontent.com/django/django/main/LICENSE). Copyright Django Software Foundation and individual contributors.
Cite this
iinventedlight. 2026. "Django class-based views: as_view and dispatch." iinventedlight Library, iil:note:kjt3baky, version of 2026-11-18. https://iinventedlight.com/library/notes/django-class-based-views/. License: CC-BY-4.0.
@misc{iil_note_kjt3baky,
author = {{iinventedlight}},
title = {{Django class-based views: as\_view and dispatch}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/django-class-based-views/},
note = {Permanent ID iil:note:kjt3baky. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- django-class-based-views.table.json (table; CC-BY-4.0)
- django-class-based-views.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/django-class-based-views.md.