MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Function-Based Views & Class-Based Views0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
2.1Installing DRF2.2Serializers Overview2.3Function-Based Views & Class-Based Views
HomeTutorialsREST API Development with DjangoFunction-Based Views & Class-Based Views
Create, Retrieve, Update, Delete APIs

Function-Based Views & Class-Based Views

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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).
1# api/views.py
2from rest_framework.decorators import api_view, permission_classes
3from rest_framework.permissions import IsAuthenticated
4from rest_framework.response import Response
5from rest_framework import status
6from .models import Product
7from .serializers import ProductSerializer
8 
9@api_view(["GET", "POST"])
10@permission_classes([IsAuthenticated])
11def product_collection(request):
12 if request.method == "GET":
13 products = Product.objects.filter(is_active=True)
14 serializer = ProductSerializer(products, many=True)
15 return Response(serializer.data)
16 
17 elif request.method == "POST":
18 serializer = ProductSerializer(data=request.data)
19 if serializer.is_valid():
20 serializer.save()
21 return Response(serializer.data, status=status.HTTP_201_CREATED)
22 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:

1# api/views.py
2from rest_framework.views import APIView
3from rest_framework.response import Response
4from rest_framework import status
5from rest_framework.permissions import IsAuthenticated
6from django.shortcuts import get_object_or_404
7from .models import Product
8from .serializers import ProductSerializer
9 
10class ProductDetailAPIView(APIView):
11 permission_classes = [IsAuthenticated]
12 
13 def get_object(self, pk):
14 return get_object_or_404(Product, pk=pk)
15 
16 def get(self, request, pk):
17 product = self.get_object(pk)
18 serializer = ProductSerializer(product)
19 return Response(serializer.data)
20 
21 def patch(self, request, pk):
22 product = self.get_object(pk)
23 # partial=True enables partial PATCH updates!
24 serializer = ProductSerializer(product, data=request.data, partial=True)
25 if serializer.is_valid():
26 serializer.save()
27 return Response(serializer.data)
28 return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
29 
30 def delete(self, request, pk):
31 product = self.get_object(pk)
32 product.delete()
33 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• Knowledge Check

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

Previous LessonNext Lesson
Serializers OverviewCreate, Retrieve, Update, Delete APIs

Practice Quiz

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

Mentor-Led Practical Training

Master REST API Development with Django with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext