Code-Memo

API View

APIView is a class-based view that provides a foundation for creating API endpoints.

1. Basic Usage

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)

2. Methods for HTTP Requests

APIView provides methods corresponding to HTTP verbs. These are:

class CreateUserView(APIView):
    def post(self, request):
        data = request.data
        return Response({'message': 'User created'}, status=status.HTTP_201_CREATED)

4. Error Handling

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)

5. Authentication and Permissions

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)

6. Customizing Request and Response Handling

You can override methods to customize request and response handling:

7. Handling Query Parameters and URL Arguments

APIView allows you to handle query parameters and URL arguments easily:

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})

8. Custom Headers and Status Codes

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)