APIView
is a class-based view that provides a foundation for creating API endpoints.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
class HelloWorldView(APIView):
def get(self, request):
return Response({'message': 'Hello, world!'}, status=status.HTTP_200_OK)
APIView
provides methods corresponding to HTTP verbs. These are:
get(self, request, *args, **kwargs)
post(self, request, *args, **kwargs)
put(self, request, *args, **kwargs)
patch(self, request, *args, **kwargs)
delete(self, request, *args, **kwargs)
class CreateUserView(APIView):
def post(self, request):
data = request.data
return Response({'message': 'User created'}, status=status.HTTP_201_CREATED)
from rest_framework.exceptions import NotFound
class GetUserView(APIView):
def get(self, request, *args, **kwargs):
user_id = kwargs.get('user_id')
if not user_exists(user_id):
raise NotFound('User not found')
return Response({'user': 'user_data'}, status=status.HTTP_200_OK)
from rest_framework.permissions import IsAuthenticated
class ProtectedView(APIView):
def get_permissions(self):
return [IsAuthenticated()]
def get(self, request):
return Response({'message': 'You are authenticated'}, status=status.HTTP_200_OK)
You can override methods to customize request and response handling:
initial(self, request, *args, **kwargs)
: Called before dispatching to method handlers. Useful for common setup or preprocessing.finalize_response(self, request, response, *args, **kwargs)
: Allows modification of the response before returning it.APIView
allows you to handle query parameters and URL arguments easily:
request.query_params
.kwargs
.class MyView(APIView):
def get(self, request, *args, **kwargs):
query_param = request.query_params.get('param', 'default')
url_arg = kwargs.get('id', 'no-id')
# Process query_param and url_arg
return Response({'param': query_param, 'id': url_arg})
class MyView(APIView):
def get(self, request):
headers = {'X-Custom-Header': 'value'}
return Response({'message': 'Custom header set'}, headers=headers, status=status.HTTP_200_OK)