Last modified: Mar 10, 2025 By Alexander Williams

How to Install Django Rest Framework Step by Step

Django Rest Framework (DRF) is a powerful tool for building Web APIs in Django. It simplifies the process of creating APIs. This guide will walk you through the installation process.

Prerequisites

Before installing DRF, ensure you have Python and Django installed. You can check their versions using the following commands:


python --version
django-admin --version

If you don't have Django installed, you can install it using pip:


pip install django

Step 1: Install Django Rest Framework

To install DRF, use pip. Run the following command in your terminal:


pip install djangorestframework

This command will download and install the latest version of DRF.

Step 2: Add DRF to Your Django Project

After installation, add DRF to your Django project. Open your settings.py file and add 'rest_framework' to the INSTALLED_APPS list:


INSTALLED_APPS = [
    ...
    'rest_framework',
    ...
]

This step ensures that Django recognizes DRF as part of your project.

Step 3: Configure DRF Settings

You can configure DRF settings in the settings.py file. For example, you can set default permissions:


REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ]
}

This configuration ensures that only authenticated users can access your API endpoints.

Step 4: Create Your First API View

Now, let's create a simple API view. Open your views.py file and add the following code:


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

class HelloWorldView(APIView):
    def get(self, request):
        return Response({"message": "Hello, World!"})

This view returns a JSON response with a "Hello, World!" message.

Step 5: Map the View to a URL

Next, map the view to a URL. Open your urls.py file and add the following code:


from django.urls import path
from .views import HelloWorldView

urlpatterns = [
    path('hello/', HelloWorldView.as_view(), name='hello_world'),
]

This code maps the HelloWorldView to the /hello/ URL.

Step 6: Run the Development Server

Finally, run the Django development server to test your API:


python manage.py runserver

Open your browser and navigate to http://127.0.0.1:8000/hello/. You should see the "Hello, World!" message.

Conclusion

You have successfully installed and configured Django Rest Framework. You also created a simple API view and mapped it to a URL. DRF is a powerful tool for building APIs in Django. For more advanced usage, check out our guides on Django Rest Framework ListCreateAPIView Example, Django Rest Framework IsAdminUser Permission Examples, and Django Rest Framework's APIView: Examples and Usage.