APIView is one of the most important classes in Django REST Framework (DRF). It provides a powerful way to handle HTTP requests while giving you more control than traditional Django views.
If you’re building custom APIs, understanding APIView is essential because many advanced DRF views are built on top of it.
What is APIView?
APIView is the base class for all class-based views in DRF. It extends Django’s standard View class and adds features such as:
- Request parsing
- Response rendering
- Authentication
- Permissions
- Throttling
- Exception handling
Instead of returning Django’s HttpResponse, DRF uses Response objects.
Importing APIView
from rest_framework.views import APIView
from rest_framework.response import Response
Basic APIView Example
from rest_framework.views import APIView
from rest_framework.response import Response
class HelloAPIView(APIView):
def get(self, request):
return Response({
"message": "Hello from APIView"
})
URL Configuration
from django.urls import path
from .views import HelloAPIView
urlpatterns = [
path('hello/', HelloAPIView.as_view()),
]
Request:
GET /api/hello/
Response:
{
"message": "Hello from APIView"
}
Handling Multiple HTTP Methods
One of the biggest advantages of APIView is handling multiple request methods within the same class.
from rest_framework.views import APIView
from rest_framework.response import Response
class EmployeeAPIView(APIView):
def get(self, request):
return Response({
"message": "Employee List"
})
def post(self, request):
return Response({
"message": "Employee Created"
})
GET Request
GET /api/employees/
Response:
{
"message": "Employee List"
}
POST Request
POST /api/employees/
Response:
{
"message": "Employee Created"
}
Accessing Request Data
For POST, PUT, and PATCH requests, use request.data.
from rest_framework.views import APIView
from rest_framework.response import Response
class EmployeeAPIView(APIView):
def post(self, request):
name = request.data.get("name")
return Response({
"employee_name": name
})
Request:
{
"name": "Tarun Kumar"
}
Response:
{
"employee_name": "Tarun Kumar"
}
Accessing Query Parameters
Use request.query_params to access URL query parameters.
class SearchAPIView(APIView):
def get(self, request):
keyword = request.query_params.get("keyword")
return Response({
"keyword": keyword
})
Request:
/api/search/?keyword=django
Response:
{
"keyword": "django"
}
Using Status Codes
DRF provides built-in HTTP status codes.
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
class EmployeeCreateAPIView(APIView):
def post(self, request):
return Response(
{"message": "Employee created successfully"},
status=status.HTTP_201_CREATED
)
Adding Authentication
APIView supports authentication directly.
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
class ProfileAPIView(APIView):
permission_classes = [IsAuthenticated]
def get(self, request):
return Response({
"user": request.user.username
})
Only authenticated users can access this endpoint.
When to Use APIView
Use APIView when:
- You need full control over request handling.
- Custom business logic is required.
- Complex validation is involved.
- Generic views are not flexible enough.
- Building custom API endpoints.
APIView vs Django View
| Feature | Django View | APIView |
|---|---|---|
| JSON Response Support | Manual | Automatic |
| Authentication | Manual | Built-in |
| Permissions | Manual | Built-in |
| Request Parsing | Limited | Advanced |
| Browsable API | No | Yes |
| Exception Handling | Manual | Built-in |
Conclusion
APIView is the foundation of many DRF views. It provides powerful request handling, built-in authentication, permissions, status codes, and JSON responses, making it the preferred choice for building custom REST APIs.