Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mock

API Automated Testing

Sign up for free

How to Install Django REST Framework

Start for free
Contents
Home / Basic Knowledge / How to Install Django REST Framework

How to Install Django REST Framework

Django Rest Framework (DRF) is a powerful and flexible toolkit for building Web APIs in Django. Here is a step-by-step guide on how to install and configure DRF in your Django project.

Django Rest Framework (DRF) is a powerful and flexible toolkit for building Web APIs in Django. Here are some of the key differences between Django and Django Rest Framework:

  • Django is a full-featured web framework for building web applications, while DRF is specifically focused on creating REST APIs.
  • Django provides server-side generated HTML responses, whereas DRF generates JSON responses.
  • Django has class-based views for request handling, while DRF has class-based views focused on API views and endpoints.
  • Django has user authentication built-in, while DRF has custom permission classes for API authentication and authorization.
  • Django templates are used for HTML views, but DRF uses serializers for converting JSON data.

You can easily create RESTful APIs with Django Rest Framework. Here is a step-by-step guide on how to install and configure DRF in your Django project.

Prerequisites

Before installing DRF, make sure you have:

  • Python 3.5+
  • Django 2.0+
  • Virtualenv (recommended)

Creating a Virtual Environment

It's recommended to install DRF in a virtual environment to avoid cluttering your system-wide Python.

On Windows:

python -m venv env 
env\Scripts\activate

On Mac/Linux:

python3 -m venv env
source env/bin/activate

This will create an isolated Python environment named env and activate it.

Installing Django Rest Framework

Use pip to install DRF:

pip install djangorestframework

This will install the latest stable version of DRF.

Adding Django Rest Framework to INSTALLED_APPS

Open settings.py in your Django project and add rest_framework to INSTALLED_APPS:

# settings.py

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

This will enable DRF in your Django project.

Configuring Settings

You may want to add/modify some DRF settings in settings.py:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ]
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ]
}

Creating API Views

With DRF installed, you can now start building API views in views.py:

from rest_framework import generics
from .models import Book 
from .serializers import BookSerializer
class BookListView(generics.ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

See the DRF Tutorial for more details on creating API views.

That's it! DRF is now installed and configured. Refer to the DRF documentation for more information on building powerful Web APIs.