Today’s Offer Enroll today and get access to premium content.
App Store Google Play
Intermediate

What are Django class-based views?

Django class-based views provide an object-oriented way to handle requests. They encapsulate behavior in classes rather than functions. Here's how to create one:

  1. Import the necessary class from django.views.
  2. Define your view by subclassing View.
  3. Define methods like get() and post() to handle requests.

Example:

from django.views import View

class MyView(View):
    def get(self, request):
        return HttpResponse('Hello, World!')

This view responds to GET requests with a simple message.