Django REST Framework Intro
Django REST Framework (DRF) is a powerful toolkit built on top of Django for building Web APIs — it turns your Django models into JSON endpoints that web and mobile clients can consume, handling serialization, request parsing, authentication, and browsable API documentation for you.
Learn Django REST Framework Intro in our free Django course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Django course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll install DRF, learn what a REST API and JSON really are, write your first API view with the @api_view decorator, and explore status codes, the Request and Response objects, and the browsable API.
A REST API is a way for programs to talk to each other over HTTP. Instead of returning an HTML page meant for a human to read, an API returns structured data — usually JSON — that another program can parse. A React front-end or a mobile app sends a request to a URL and gets back data it can display however it likes.
JSON (JavaScript Object Notation) looks almost exactly like a Python dictionary. A single user might be represented as {' '} . Because JSON maps so cleanly onto Python dicts and lists, DRF can convert your models into JSON with very little work from you.
Django REST Framework is installed with pip , then registered in your project's INSTALLED_APPS so Django loads it.
In plain Django you write views that return an HttpResponse . In DRF you decorate a function with @api_view and return a DRF Response object. DRF then takes care of converting your Python data into JSON automatically.
The @api_view decorator takes a list of the HTTP methods the view accepts, such as ['GET'] or ['GET', 'POST'] . If a client uses a method that isn't in the list, DRF returns a 405 Method Not Allowed response for you.
Visiting /api/hello/ now returns JSON like {' '} . Notice you returned a Python dict — DRF's Response handled the JSON conversion.
Every HTTP response carries a status code that tells the client what happened. DRF gives you readable constants in the status module so you don't have to memorize raw numbers like 201 or 404.
Inside the view, DRF gives you an enhanced Request object: request.method is the HTTP method, and request.data holds parsed JSON sent by the client (no manual parsing needed). You build a Response by passing your data and, optionally, a status code.
Fill in the blanks so this view only allows GET requests and returns its data correctly. Replace each ___ .
❌ The browsable API and DRF features don't work
You forgot to add 'rest_framework' to INSTALLED_APPS in settings.py.
✅ Fix: add "rest_framework" to the INSTALLED_APPS list and restart the server.
❌ Returning JsonResponse instead of DRF's Response
Using Django's JsonResponse skips DRF's renderers, so you lose the browsable API and content negotiation.
✅ Fix: import from rest_framework.response import Response and return Response(data) instead.
The HTTP method the client used isn't listed in your @api_view decorator.
✅ Fix: include the method, e.g. @api_view(['GET', 'POST']) to allow both GET and POST.
Build a function that acts like an API endpoint: it takes a request method, returns a list of books for GET, and a 201-style response when a new book is posted.
Lesson 19 complete — you built your first DRF API!
You installed Django REST Framework, registered it in INSTALLED_APPS , learned what REST APIs and JSON are, wrote a view with @api_view that returns a Response , and used status codes plus the browsable API.
🚀 Up next: Serializers & ViewSets — learn how to convert your Django models to and from JSON automatically and build full CRUD APIs with far less code.
Practice quiz
What does Django REST Framework primarily help you build?
- HTML templates
- Web APIs that return JSON
- Database migrations
- CSS stylesheets
Answer: Web APIs that return JSON. DRF builds Web APIs on top of Django, returning JSON instead of rendered HTML.
Which decorator turns a function into a DRF API view?
- @login_required
- @csrf_exempt
- @api_view
- @require_GET
Answer: @api_view. @api_view marks a function as a DRF view and handles JSON conversion.
What does the @api_view decorator take as its argument?
- GET
- POST
Answer: GET. @api_view(['GET', 'POST']) lists which HTTP methods the view accepts.
What object should a DRF API view return?
- An HttpResponse
- A render() call
- A JsonResponse from Django
- A DRF Response object
Answer: A DRF Response object. DRF views return a Response object, which DRF serializes to JSON automatically.
What status code does DRF return if a client uses a method not in @api_view?
- 405 Method Not Allowed
- 404 Not Found
- 500 Server Error
- 200 OK
Answer: 405 Method Not Allowed. DRF returns 405 Method Not Allowed when the method isn't listed in the decorator.
What is JSON?
- A Python web server
- A lightweight text data format that maps to dicts and lists
- A Django template engine
- A type of database
Answer: A lightweight text data format that maps to dicts and lists. JSON is a lightweight, text-based format that maps cleanly to Python dicts and lists.
Is DRF part of Django core or a separate package?
- Built into Django core
- Only in Django 5+
- A third-party package installed on top of Django
- Part of the standard library
Answer: A third-party package installed on top of Django. DRF is a third-party package added to INSTALLED_APPS, sitting on top of Django.
What status code constant signals a successful creation in DRF?
- status.HTTP_404_NOT_FOUND
- status.HTTP_500_INTERNAL_SERVER_ERROR
- status.HTTP_204_NO_CONTENT
- status.HTTP_201_CREATED
Answer: status.HTTP_201_CREATED. 201 Created signals a resource was successfully created; DRF exposes it as status.HTTP_201_CREATED.
What handy feature does DRF provide for testing endpoints in a browser?
- The browsable API
- A SQL console
- A migration viewer
- A template debugger
Answer: The browsable API. DRF ships a browsable HTML API interface for exploring and testing endpoints.
Should you know Django basics before learning DRF?
- No, DRF replaces Django entirely
- Yes — DRF builds on models, URLs, and views
- Only the admin is required
- Only templates are required
Answer: Yes — DRF builds on models, URLs, and views. DRF builds directly on Django models, URL patterns, and views.