Skip to content

7. A JSON API with Django REST Framework

Our site currently returns only HTML rendered from templates. To support a modern JavaScript frontend framework, we'll build an API with Django REST Framework (DRF).

Why DRF?

Django's JsonResponse already returns JSON, but DRF also converts model instances, validates the request body, returns errors with the correct HTTP status codes, and more.

We'll build a read-only API for the animals with:

  • Model serializers: Converting an animal from Python to JSON.
  • Viewsets and routers: Serving the list and each animal's detail from one class, and a router for the URL patterns.
  • The browsable API: Interacting with the API in a browser during development.