Function-Based Views & Class-Based Views0%

Function-Based Views & Class-Based Views

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Function-Based Views & Class-Based Views

In Django REST Framework, you can build API endpoints using either Function-Based Views with the @api_view decorator or Class-Based Views inheriting from APIView. Choosing between them depends on whether your endpoint represents simple procedural logic or an object-oriented controller with reusable behaviors.


1. Function-Based Views with @api_view

The @api_view decorator wraps standard Python functions, giving them DRF superpowers:

  • Parses incoming request bodies into request.data.
  • Enforces permitted HTTP methods.
  • Enables content negotiation (JSON vs Browsable API).
Python
# api/views.py
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework import status
from .models import Product
from .serializers import ProductSerializer
 
@api_view(["GET", "POST"])
@permission_classes([IsAuthenticated])
def product_collection(request):
if request.method == "GET":
products = Product.objects.filter(is_active=True)
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)
 
elif request.method == "POST":
serializer = ProductSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

2. Class-Based Views with APIView

APIView organizes HTTP methods into dedicated class methods (get(), post(), put(), delete()), eliminating messy if request.method == "..." branches:

Python
# api/views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from django.shortcuts import get_object_or_404
from .models import Product
from .serializers import ProductSerializer
 
class ProductDetailAPIView(APIView):
permission_classes = [IsAuthenticated]
 
def get_object(self, pk):
return get_object_or_404(Product, pk=pk)
 
def get(self, request, pk):
product = self.get_object(pk)
serializer = ProductSerializer(product)
return Response(serializer.data)
 
def patch(self, request, pk):
product = self.get_object(pk)
# partial=True enables partial PATCH updates!
serializer = ProductSerializer(product, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 
def delete(self, request, pk):
product = self.get_object(pk)
product.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

3. Comparison Matrix

Feature@api_view (Functional)APIView (Class-Based)
Code Organizationif/elif on request.methodDistinct def get(), def post() methods
Inheritance & MixinsNot supportedSupported (OOP inheritance)
URL Registrationpath("url/", views.my_view)path("url/", views.MyView.as_view())
Best Used ForOne-off calculations, simple webhooksResource endpoints, CRUD logic, OOP reuse

4. Understanding request.data

Unlike standard Django's request.POST (which only parses form-encoded data), DRF's request.data:

  • Handles incoming JSON payloads (application/json).
  • Handles standard form submissions (multipart/form-data, application/x-www-form-urlencoded).
  • Handles file uploads transparently without inspecting request.FILES separately!

Interactive Quiz

Practice Quiz

Question 1
What does the @api_view(["GET", "POST"]) decorator do to a Python function?
Question 2
How does APIView separate HTTP method logic compared to @api_view?
Question 3
What advantage does DRF's request.data have over standard Django's request.POST?
Question 4
In an APIView patch method, what argument enables partial updates in the serializer?
Question 5
How must an APIView class be referenced inside a urls.py path pattern?
Next Lesson

Create, Retrieve, Update, Delete APIs

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext