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 helperdjact_render().djact/middleware.py— attachesrequest.is_djactandrequest.djact.djact/utils.py— helpers (header detection and CSRF).djact/templates/djact.html— HTML shell with#appmount 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.htmlis used bydjact_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.jsxis a minimal component.index.jsshowsresolve()andbootstrap().
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
- Django view calls
djact_render(). - Server builds payload:
component(string name)props(data dict)url(current path)
- First load returns full HTML shell.
- Payload is embedded in
data-page. - Client runtime reads
data-pageand mounts React. - Next navigation sends
X-Djact: true. - 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-Locationon redirects for Djact requests. - The client runtime listens to redirects and does a hard reload when needed.
Make sure:
- You are using
DjactMiddleware. - Django login view returns a normal redirect (e.g.
return redirect("/")). - Your frontend uses
LinkordjactVisit()soX-Djactheader 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
-
djactinstalled -
DjactMiddlewareenabled -
djact_render()used in views -
djact/static/app.jsbuilt - Template loads and React mounts
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c695f6388e7381527041435878ffb5325c90c7dbd8b4955343151f177abd1af2
|
|
| MD5 |
80ba386a0c1bfbf322f84303c37cb3d8
|
|
| BLAKE2b-256 |
40dd870f577aaf646404c830b44d290d3e380ea28c4ce14b53223325f005a7be
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d4c741e6a839eeac1a824196c27e4b429dc0e1d372278b6ccd45aefb28fd17b
|
|
| MD5 |
72cc1e0f52c918b29391b5432d4dbd03
|
|
| BLAKE2b-256 |
714d2eb1f8f87c085cb81700f08feef78cee0c5ce170b937a90f3787f332a600
|