← Back to Django Course | Chapter 14: REST API with DRF | Lesson 1 of 6

Introduction to Django REST Framework

DRF is a toolkit that turns your Django models into JSON data other apps and websites can read and update.

What Problem Does DRF Solve?

A plain Django view returns HTML. Many apps (mobile apps, JavaScript frontends, other services) instead need data as JSON. DRF gives you the tools to expose your models as a clean JSON API without writing JSON parsing and validation logic by hand.

Note: Regular Django views and DRF API views can live side by side in the same project.

Example: What Problem Does DRF Solve?

A plain Django view returns HTML. Many apps (mobile apps, JavaScript frontends, other services) instead need data as JSON. DRF gives you the tools to expose your models as a clean JSON API without writing JSON parsing and validation logic by hand.

markup
# A plain Django view returns HTML for humans:
def book_list(request):
    books = Book.objects.all()
    return render(request, 'books/list.html', {'books': books})

# DRF instead returns JSON for programs to consume,
# built from the same Book model using a serializer + API view.
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

DRF Is Built on Django

DRF does not replace Django's models, URLs, or settings — it adds new pieces (serializers and API views) that work alongside them. Your Book model from earlier chapters can be reused directly with DRF.

Example: DRF Is Built on Django

DRF does not replace Django's models, URLs, or settings — it adds new pieces (serializers and API views) that work alongside them. Your Book model from earlier chapters can be reused directly with DRF.

markup
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=100)
    published_year = models.IntegerField()

    def __str__(self):
        return self.title
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

The Browsable API

One of DRF's most useful features is the browsable API: visiting an API endpoint in a normal web browser shows a readable HTML page with the JSON data and forms to try requests, instead of raw text.

Note: The browsable API is only shown to browsers; other clients (like curl or a mobile app) still get plain JSON.

Example: The Browsable API

One of DRF's most useful features is the browsable API: visiting an API endpoint in a normal web browser shows a readable HTML page with the JSON data and forms to try requests, instead of raw text.

markup
# No code needed to see this -- once a DRF view is wired up,
# visiting its URL in a browser (e.g. /api/books/) shows:
# - the JSON response
# - a form to POST new data
# - clickable links to related endpoints
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Trying to build a JSON API by hand-writing HttpResponse(json.dumps(...)) instead of using DRF's serializers and views, which handle validation, content negotiation, and errors for you.
  2. Assuming DRF is a separate framework from Django, when it is actually a library that plugs into your existing Django project and models.
  3. Skipping the rest_framework entry in INSTALLED_APPS and then being confused when DRF's browsable API or generic views fail to work.
Chapter Summary
  • Django REST Framework (DRF) is a library built on top of Django for creating Web APIs.
  • It reuses your existing Django models and adds serializers, API views, and browsable HTML output.
  • DRF must be installed separately and added to INSTALLED_APPS before it can be used.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.