Skip to main content

Inertia.js-like bridge between Django and React — server-driven SPA without a REST API.

Project description

Djact

Djact is a minimal Django + React bridge. It lets Django render the first HTML page and lets React handle fast navigation afterward without a REST API.

Package name

pip install djact

Supports

  • Python: 3.10+
  • Django: 4.x and 5.x (package requirement is Django >= 4.0)
  • React: 18+

Final folder structure (inside this package)

djact/
├── djact/
│   ├── __init__.py
│   ├── apps.py
│   ├── middleware.py
│   ├── render.py
│   ├── utils.py
│   ├── static/
│   │   └── app.js
│   ├── templates/
│   │   └── djact.html
│   └── djact/
│       ├── App.jsx
│       └── index.js
├── pyproject.toml
└── README.md

What each file does

  • djact/render.py — server helper djact_render().
  • djact/middleware.py — attaches request.is_djact and request.djact.
  • djact/utils.py — helpers (header detection and CSRF).
  • djact/templates/djact.html — HTML shell with #app mount point.
  • djact/static/app.js — client runtime (ES module).
  • djact/djact/App.jsx — example React component.
  • djact/djact/index.js — example resolver + bootstrap.

Step-by-step setup (Django project)

1) Install package

pip install djact

2) Add app + middleware in settings

INSTALLED_APPS = [
    # ...
    "django.contrib.staticfiles",
    "djact",
]

MIDDLEWARE = [
    # ...
    "djact.middleware.DjactMiddleware",
]

3) Create a Django view

from djact.render import djact_render

def home(request):
    return djact_render(request, "Home", {"message": "Hello from Django"})

4) URL routing

from django.urls import path
from .views import home

urlpatterns = [
    path("", home, name="home"),
]

5) Template behavior (no extra work needed)

  • djact/templates/djact.html is used by djact_render().
  • It renders <div id="app" data-page="..."> where JSON is embedded.
  • It loads the client runtime via {% static 'app.js' %}.

6) Frontend example files

  • Example React code is already at djact/djact/.
  • App.jsx is a minimal component.
  • index.js shows resolve() and bootstrap().

You can replace these with your own pages later.

React setup (minimal npm)

You must bundle React into djact/static/app.js.

Option A: Vite (recommended)

npm init -y
npm install react react-dom
npm install -D vite @vitejs/plugin-react

Create vite.config.js:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
  build: {
    outDir: "./djact/static",
    rollupOptions: {
      output: {
        entryFileNames: "app.js",
        chunkFileNames: "app.js",
        assetFileNames: "[name].[ext]",
      },
    },
  },
});

Example entry src/main.js:

import { bootstrap } from "../djact/djact/index.js";
bootstrap();

Build:

npx vite build

Option B: Any bundler

  • Output must be djact/static/app.js.
  • Template already loads {% static 'app.js' %}.

How render + data flow works

  1. Django view calls djact_render().
  2. Server builds payload:
    • component (string name)
    • props (data dict)
    • url (current path)
  3. First load returns full HTML shell.
  4. Payload is embedded in data-page.
  5. Client runtime reads data-page and mounts React.
  6. Next navigation sends X-Djact: true.
  7. Server returns JSON and the client swaps the component.

Passing data

return djact_render(request, "Home", {"user": "Ankur", "count": 12})

Shared props for every page

request.djact.share("authUser", {"name": "Ankur"})

Login redirect problem (common issue) and fix

If login or auth redirect is not working in Djact navigation:

  • The middleware adds X-Djact-Location on redirects for Djact requests.
  • The client runtime listens to redirects and does a hard reload when needed.

Make sure:

  1. You are using DjactMiddleware.
  2. Django login view returns a normal redirect (e.g. return redirect("/")).
  3. Your frontend uses Link or djactVisit() so X-Djact header is sent.

If login still fails, share the error log and I will fix it.

Run Django

python manage.py runserver

Open: http://127.0.0.1:8000/

Final checklist

  • djact installed
  • DjactMiddleware enabled
  • djact_render() used in views
  • djact/static/app.js built
  • Template loads and React mounts

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djact-1.0.1.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djact-1.0.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file djact-1.0.1.tar.gz.

File metadata

  • Download URL: djact-1.0.1.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for djact-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c695f6388e7381527041435878ffb5325c90c7dbd8b4955343151f177abd1af2
MD5 80ba386a0c1bfbf322f84303c37cb3d8
BLAKE2b-256 40dd870f577aaf646404c830b44d290d3e380ea28c4ce14b53223325f005a7be

See more details on using hashes here.

File details

Details for the file djact-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: djact-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for djact-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d4c741e6a839eeac1a824196c27e4b429dc0e1d372278b6ccd45aefb28fd17b
MD5 72cc1e0f52c918b29391b5432d4dbd03
BLAKE2b-256 714d2eb1f8f87c085cb81700f08feef78cee0c5ce170b937a90f3787f332a600

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page