Django REST Framework

DRF is the standard toolkit for building powerful web APIs.

For anything beyond the basics, use Django REST Framework (DRF). It adds serializers, class-based API views, authentication, permissions and a browsable API interface.

Install it, add it to INSTALLED_APPS, and you are ready to build.

Example

Example · bash
# Install
pip install djangorestframework

# settings.py
# INSTALLED_APPS = [
#     ...
#     "rest_framework",
# ]

When to use it

  • A team installs Django REST Framework to build a versioned API for a mobile app backend.
  • A developer uses DRF's browsable API during development to test endpoints directly in the browser.
  • A team configures DRF's DEFAULT_AUTHENTICATION_CLASSES to use JWT tokens for stateless API authentication.

More examples

Install and configure DRF

Installs DRF via pip and registers it as an installed app to activate its components.

Example · bash
pip install djangorestframework
# Then add to settings.py:
# INSTALLED_APPS = [..., 'rest_framework']

Global DRF settings

Configures default authentication, permission, and pagination classes for all DRF views.

Example · python
# settings.py
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework.authentication.SessionAuthentication",
    ],
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
    ],
    "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
    "PAGE_SIZE": 20,
}

First DRF API view

Uses the @api_view decorator to create a simple DRF endpoint that returns a Response object.

Example · python
from rest_framework.decorators import api_view
from rest_framework.response import Response

@api_view(["GET"])
def hello(request):
    return Response({"message": "Hello from DRF!"})

Discussion

  • Be the first to comment on this lesson.