Home > database >  Why use Django REST Framework instead of returning normal HttpResponse in views?
Why use Django REST Framework instead of returning normal HttpResponse in views?

Time:10-29

I'm new to Python and I've seen a lot of tutorials which jump into the Django REST Framework when discussing how to create REST APIs without explaining why they need this library. I don't see the purpose of using the Django Rest Framework when I can just define API endpoints in views and return a simple HttpResponse to send data to a client.

What does the Django Rest Framework accomplish that I can't do simply using HttpResponse? Why is it worth learning?

I was going to use the library as it was included in the video, but it seemed more complex than I needed and I decided to try creating an API without Django REST Framework

def getStats(request):
    print('--------STARTING Stats----------')
    
    # Take some GET variable
    version = request.GET.get('version')
    
    # Get some data
    with open('static/data.json') as f:
        data = json.load(f)
        
    # Filter the data
    if version is not None:
        data = list(filter(lambda x: x['version'] == version, data))
        print("FILTERED DATA", len(data))
    
    # Perform some operations on the data
    data = calculateStats(data)
    
    # Return an HTTP response
    return HttpResponse(json.dumps(data))

This code seemed to work as needed and I get the feeling that I could make this view more robust if needed based on the demands of my application.

CodePudding user response:

The DRF is a complete toolkit for building RESTful APIs in Django, including authentication, serialization, etc. You can do without it but, in the end of the day, the DRF will save you a lot of time.

It fits well with the Django architecture and it's also quite extensible, so you won't be locked-in to some obscure extension, as it happens sometimes.

Perhaps this old reddit post from Tom Christie (DRF's creator) can shed some more light:

Django REST framework isn't required, but it helps you get a lot of things right that will be time consuming and error prone if you're working from core Django.

CodePudding user response:

Your example is very simple. When you have more complex logic with orm, permissions, serialization, and others. You will see the benefits of django restframework. You can build a crud very fast using viewsets that guarantee permissions, filters, with the REST API structure using POST, GET, PATCH, PUT, DELETE. You can list, create, update and delete. I recommend that you do the restframework tutorial https://www.django-rest-framework.org/tutorial/quickstart/ and it will become clear to you if is the right choice or not.

You can also use it for your example like this:

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

    @api_view(['GET'])
    def getStats(request):
        print('--------STARTING Stats----------')
    
        # Take some GET variable
        version = request.GET.get('version')
    
        # Get some data
        with open('static/data.json') as f:
            data = json.load(f)
    
        # Filter the data
        if version is not None:
            data = list(filter(lambda x: x['version'] == version, data))
            print("FILTERED DATA", len(data))
    
        # Perform some operations on the data
        data = calculateStats(data)
    
        # Return an HTTP response
        return Response(json.dumps(data, status=status.HTTP_200_OK)
  • Related