Django Rest Framework (DRF) provides built-in support for handling HTTP status codes in API responses.

The DRF API view returns a response object containing an HTTP status code when someone requests it. You can set the status code in the response manually or the request processing result can determine it automatically.

The framework automatically handles HTTP status codes in Django Rest Framework. However, you can customize the status codes by using the status module from the Django Rest Framework.

Examples of Handling HTTP Codes

Here are some examples of handling HTTP status codes in Django Rest Framework:

  1. Return a 200 OK status code with a response:
from rest_framework.response import Response
from rest_framework import status

def my_view(request):
    data = {'message': 'Success!'}
    return Response(data, status=status.HTTP_200_OK)

2. Return a 400 Bad Request status code with an error message:

from rest_framework.response import Response
from rest_framework import status

def my_view(request):
    data = {'message': 'Error!'}
    return Response(data, status=status.HTTP_400_BAD_REQUEST)

Return a 404 Not Found status code with a custom error message:

from rest_framework.exceptions import NotFound

def my_view(request):
    raise NotFound('Resource not found')

By default, Django Rest Framework handles the following HTTP status codes:

  • 200 OK
  • 201 Created
  • 204 No Content
  • 400 Bad Request
  • 401 Unauthorized
  • 403 Forbidden
  • 404 Not Found
  • 405 Method Not Allowed
  • 406 Not Acceptable
  • 415 Unsupported Media Type
  • 500 Internal Server Error

You can also create your own custom status codes by using the status.HTTP_XXX format, where XXX is the status code number.

Here are some additional ways to handle HTTP status codes in Django Rest Framework:

  1. Using the @api_view decorator:

The @api_view decorator is used to specify the allowed HTTP methods for a view and the corresponding response codes. For example:

from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status

@api_view(['GET'])
def my_view(request):
    if request.method == 'GET':
        data = {'message': 'Success!'}
        return Response(data, status=status.HTTP_200_OK)

In this example, the view is allowed only for the GET method and returns a 200 OK status code.

  1. Using the APIView class:

The APIView class is a base class for creating API views in Django Rest Framework. You can define the allowed HTTP methods and their corresponding response codes in the http_method_names and status_codes attributes, respectively. For example:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

class MyView(APIView):
    http_method_names = ['get']
    status_codes = {
        'get': status.HTTP_200_OK,
    }

    def get(self, request):
        data = {'message': 'Success!'}
        return Response(data, status=status.HTTP_200_OK)

In this example, the view is allowed only for the GET method and returns a 200 OK status code.

  1. Using custom exceptions:

You can raise custom exceptions to handle HTTP status codes in a more granular way. For example:

from rest_framework.exceptions import APIException

class MyException(APIException):
    status_code = status.HTTP_400_BAD_REQUEST
    default_detail = 'Error!'

@api_view(['GET'])
def my_view(request):
    if some_error_condition:
        raise MyException()
    data = {'message': 'Success!'}
    return Response(data, status=status.HTTP_200_OK)

In this example, the custom exception MyException is raised when some error condition is met. The exception returns a 400 Bad Request status code with the default message “Error!”.

Github link :

https://github.com/saikumar248