← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:kjt3baky

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.},
}

Markdown

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.

All artifact files (JSON index)

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.