Skip to main content

🐍 Introduction

Django Headless turns Django into the backend of a headless CMS. Decorate a model with @expose() and a Django REST Framework API appears, with list, detail, create, update and delete routes, filtering, pagination and singletons. You write no serializers, viewsets or URL patterns.

# blog/models.py
from django.db import models
from headless import expose


@expose()
class Article(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
published = models.BooleanField(default=False)

This decorator generates these routes:

MethodRouteAction
GET/api/blog.articleList
POST/api/blog.articleCreate
GET/api/blog.article/1Retrieve
PUT/api/blog.article/1Update
PATCH/api/blog.article/1Partial update
DELETE/api/blog.article/1Delete
Protected by default

Generated routes answer 403 Forbidden to everyone but staff users, so an exposed model is never public by accident. To open the routes up, see Permissions.

When to use it​

Use Django Headless when Django holds your content and the frontend lives elsewhere: a Next.js or Astro site, a static site build, a mobile app. The API is predictable for such a frontend:

  • Routes per model: one decorator per model, no URL patterns to maintain.
  • Singletons: site settings and other one-off objects as a single route.
  • Filtering: every lookup a field supports, from the query string.
  • Lookup routes: fetch an object by a unique field such as a slug.
  • Stable lists: ordered by primary key, with a pagination envelope that includes ready-to-fetch links.
  • Clear errors: invalid filters and options answer with the reason.

If your editors also need an admin to manage that content, see Django Content Studio. For a production-ready Django stack with batteries included, see Django Stack.

Next steps​

Follow the installation guide to expose your first model.