← Back to Django Course | Chapter 9: Class-Based Views | Lesson 2 of 10

The Base View Class

The base View class is the empty toolbox that every other class-based view in Django is built on top of.

Subclassing View Directly

The plain View class from django.views.generic.base gives you full control: you define exactly which HTTP methods the view understands by writing get(), post(), or any other lowercase method name matching an HTTP verb.

Example: Subclassing View Directly

PingView only responds to GET requests; any other method automatically gets a 405 Method Not Allowed response.

markup
from django.http import HttpResponse
from django.views import View


class PingView(View):
    def get(self, request):
        return HttpResponse('pong')
{# 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. #}

Automatic 405 for Unsupported Methods

If a request arrives with an HTTP method the view class has not defined (for example a POST sent to a view that only defines get()), View.dispatch() automatically returns a 405 Method Not Allowed response instead of raising an error.

Note: This safety net means you never need to manually check request.method for methods you do not support.

Example: Automatic 405 for Unsupported Methods

Sending a POST request to ReadOnlyView returns Django's built-in 405 response without any extra code.

markup
from django.views import View
from django.http import HttpResponse


class ReadOnlyView(View):
    def get(self, request):
        return HttpResponse('You can only GET this page')
{# 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. #}

Accessing URL Arguments in a View Class

Just like function-based views, class-based views receive extra URL keyword arguments (captured from angle brackets in the URL pattern) as arguments to their methods, after self and request.

Example: Accessing URL Arguments in a View Class

A URL pattern like path("greet/<str:name>/", GreetView.as_view()) passes name into get() automatically.

markup
from django.http import HttpResponse
from django.views import View


class GreetView(View):
    def get(self, request, name):
        return HttpResponse(f'Hello, {name}!')
{# 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. #}
Common Mistakes
  1. Overriding dispatch() and forgetting to call super().dispatch(request, *args, **kwargs), which breaks method routing entirely.
  2. Defining a method named after an HTTP verb that Django does not recognize (e.g. "handle_get" instead of "get"), so it is silently never called.
  3. Not listing an HTTP method in http_method_names when it needs to be blocked or allowed explicitly.
Chapter Summary
  • django.views.generic.base.View is the base class every built-in generic CBV inherits from.
  • View.as_view() returns a callable that Django's URL dispatcher can invoke like a function.
  • View.dispatch() looks at request.method and calls the matching lowercase method (get, post, put, delete, etc.).
  • A method not defined on the class simply is not supported, and Django returns an HTTP 405 response automatically.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.