APIView and Function @api_view
Build endpoints two ways.
APIView and Function @api_view is a free Django Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Ways to Build Endpoints
DRF lets you write endpoints as classes with APIView or as plain functions decorated with @api_view. Both speak the same JSON dialect.
The Class Style
APIView subclasses Django's View but gives you DRF's request parsing, content negotiation, and tidy response handling for free.
from rest_framework.views import APIViewMethods Map to Verbs
In an APIView you write a method per HTTP verb: get for reads, post for creates, and so on, keeping each action clearly separated.
class BookList(APIView):
def get(self, request):
...
def post(self, request):
...Return a Response
Instead of HttpResponse, you return DRF's Response object, which renders to JSON or the browsable API based on the request.
from rest_framework.response import Response
return Response({'ok': True})Read request.data
DRF parses the body for you, so you read posted JSON from request.data as a normal Python dict, no manual decoding needed.
title = request.data['title']The Function Style
For simple endpoints the @api_view decorator turns an ordinary function into a DRF view, listing which HTTP methods it accepts.
from rest_framework.decorators import api_viewDeclare Allowed Methods
You pass the verbs to the decorator. Any method not listed gets an automatic 405 Method Not Allowed, so you do not handle it yourself.
@api_view(['GET', 'POST'])
def books(request):
...Same Response Object
Function views use the very same DRF Response, so you can mix class and function styles in one project without friction.
return Response(serializer.data)Which Style to Pick
Reach for @api_view when an endpoint is small and one-off; choose APIView when you want reusable, organized methods on a class.
Set the Status
Both styles let you pass status= to Response so clients get the right HTTP code, like 201 when you create something new.
Response(data, status=201)Same Power, Two Shapes
Whether class or function, you get DRF parsing, responses, and status handling, the difference is mostly structure and personal taste.
Quick Check
What does @api_view do to a plain function?
Recap: Two View Styles
You can build endpoints with class-based APIView or function-based @api_view, both returning a DRF Response. Pick the shape that fits. 🧩
Frequently asked questions
Is the “APIView and Function @api_view” lesson free?
Yes — the full text of “APIView and Function @api_view” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “APIView and Function @api_view”?
Build endpoints two ways. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “APIView and Function @api_view” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Installing and Configuring DRF
- Serializers: Model to JSON
- APIView and Function @api_view
- The Browsable API and Status Codes