Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 39 ยท Getting Started

What Is Django REST Framework?

Learn what Django REST Framework (DRF) is, which problems it solves for web APIs, how a request flows through it, and run a real DRF endpoint in your browser.

Django can already return JSON, so why would you add another package to build an API? Because an API is more than JsonResponse. A real API must parse request bodies in several formats, validate every field, turn database rows into JSON and back, check who is calling, limit how often they call, split long lists into pages, describe errors in a predictable shape and document itself. Writing those parts by hand for every endpoint is slow and easy to get wrong.

Django REST Framework (DRF) is the most widely used toolkit that packages all of these chores into reusable classes. It sits on top of Django: you keep your models, your URL configuration, your settings and your admin, and you add a layer that speaks JSON (and other formats) over HTTP.

How the examples on this site run. The code in this track is real Django and real Django REST Framework, installed in your browser with micropip and running on an in-memory SQLite database. There is no web server and no network port in a browser, so we call the API with DRF's own APIClient, which builds genuine requests and sends them through the full Django request cycle (URL resolver, middleware, views, serializers, renderers). Every status code and JSON body you see is what a real server would return. Commands such as pip install and manage.py runserver are shown in read-only blocks. Each live example starts with a short setup block that configures Django, because a browser tab has no settings.py file.

What DRF gives you

DRF is a collection of cooperating parts. You will meet each one in this track:

The parts of DRF

Serializers Convert objects to JSON and validate input
Views Handle requests, return responses
Routers Generate URLs for you
Auth and permissions Who are you, and may you?
1 / 2

You can use only the parts you need. A small internal API might use function views and serializers and nothing else. A large public API might use all of them, plus your own custom classes in each slot.

How a request flows through DRF

Every DRF request goes through the same pipeline. Knowing it makes the rest of the track easier, because each later lesson is about one box in this picture.

flowchart LR
    C["Client"] --> U["Django URLs"]
    U --> V["DRF view"]
    V --> N["Content negotiation<br/>parse the body"]
    N --> A["Authentication"]
    A --> P["Permissions and throttles"]
    P --> H["Handler method<br/>get / post / ..."]
    H --> S["Serializer<br/>validate and convert"]
    S --> D["Model and database"]
    H --> R["Response + renderer"]
    R --> C

A request enters Django's URL resolver, reaches a DRF view, is checked, handled with the help of serializers and returned as a rendered response.

Your first live DRF endpoint

The example below is complete: it configures Django, defines a view, registers a URL and calls it. Press Run and read the output. Do not worry about every line; the next lessons explain each piece.

Python A complete tiny API

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice three things. First, Response accepted a plain dictionary and the framework rendered it as JSON with the right Content-Type. Second, request.query_params is DRF's name for the query string. Third, the POST was rejected with 405 Method Not Allowed and a JSON error body, without you writing any code for it: the @api_view(["GET"]) decorator declared which methods are allowed.

Serializers in one minute

The most important DRF idea is the serializer. It describes the public shape of a resource and does two jobs: it converts data to simple types for output, and it validates untrusted input.

Python Output and input with one serializer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The string "1927" became the integer 1927 because the field converts what it receives. The invalid payload produced one error per field, in a dictionary you can send straight back to the client. Serializers get their own chapter.

Installing DRF in a real project

On your own computer, the setup takes four steps. These commands are read-only here because a browser cannot run a shell:

Terminal
python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install django djangorestframework
django-admin startproject mysite

Then add rest_framework to INSTALLED_APPS in settings.py:

Python
INSTALLED_APPS = [
    # ... Django's own apps ...
    "rest_framework",
    "library",          # your app
]

That is all that is required. Optional DRF settings live in one dictionary called REST_FRAMEWORK, which you will use in later chapters to choose default authentication, permissions and pagination for the whole project.

When to choose DRF

DRF is a very good fit when you already use Django, when your API mirrors database models, or when you need standard features such as token authentication, pagination and browsable documentation quickly. It is a weaker fit for tiny single-purpose services (a plain Django view or a micro-framework may be lighter) and for APIs that are mostly about long-lived connections such as WebSockets, which need a different tool. Many teams also use DRF only for the serializers and keep the rest of their stack unchanged. A common alternative is Django Ninja, which uses type hints; DRF's advantages are maturity, its enormous ecosystem and the fact that most Django developers already know it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does DRF add on top of plain Django views?

    DRF is a toolkit layered on Django. It keeps your models and URLs and adds API-focused classes.
  2. 2. Why do the examples on this site use APIClient instead of a web server?

    APIClient builds real requests and sends them through URL routing, views, serializers and renderers, so the results match a real server.
  3. 3. What does a serializer do?

    Serializers work in both directions: objects to JSON-ready data, and untrusted input to validated data or errors.
  4. 4. Which setting must you add to INSTALLED_APPS to use DRF?

    The package's app label is rest_framework, even though the project is called Django REST Framework.

Summary

  • DRF is a toolkit on top of Django for building web APIs; it does not replace models, URLs or the admin.
  • Its main parts are serializers, views, routers, authentication, permissions, pagination, filtering, throttling and schema/documentation.
  • A request passes through negotiation, authentication, permissions and a handler method before a renderer builds the response.
  • In this track DRF runs for real in your browser, called through APIClient; install and server commands are read-only.
  • Installing DRF means pip install djangorestframework and adding rest_framework to INSTALLED_APPS.

Next: Your First API View