Run a Django backend (Django ORM, Django Admin) alongside a Reflex app.
Project description
reflex-django
Keep Django. Get a reactive UI in Python. Same process, same port, same cookies.
Documentation · GitHub · PyPI
What is it?
You love Django — the ORM, the admin, migrations, the way it just works. You also want a modern, reactive UI written in Python, not React.
reflex-django runs Django and Reflex as one ASGI app on one port. Configuration lives in urls.py. Pages live in views.py. The Django session you got from /admin/login/ is the same session your Reflex button handlers see.
- Same port — Django at
8000, Reflex at8000. No CORS, no token bridge, no second dev server. - Same cookies — log in once at
/admin/, every Reflex event seesself.request.user. - Same middleware — your full
settings.MIDDLEWAREchain runs on every Reflex event. - One command —
python manage.py run_reflex.
Minimal setup
Install:
uv add django reflex reflex-django
config/settings.py:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"reflex_django",
"shop",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"reflex_django.streaming_middleware.AsyncStreamingMiddleware",
]
config/urls.py:
from django.contrib import admin
from django.urls import path
from reflex_django.urls import reflex_mount
urlpatterns = [path("admin/", admin.site.urls)]
urlpatterns += [
reflex_mount(app_name="shop", django_prefix=("/admin",)),
]
config/asgi.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
from reflex_django.asgi_entry import application # noqa: E402,F401
shop/views.py:
import reflex as rx
from reflex_django import template
from reflex_django.state import AppState
class HomeState(AppState):
@rx.event
async def on_load(self):
user = self.request.user
self.greeting = (
f"Hi, {user.get_username()}!"
if user.is_authenticated
else "Hello, guest. Log in at /admin/."
)
@template(route="/", title="Home", on_load=HomeState.on_load)
def index() -> rx.Component:
return rx.vstack(
rx.heading("My Shop"),
rx.text(HomeState.greeting),
)
Run:
python manage.py migrate
python manage.py run_reflex
Open http://localhost:8000/. Admin at http://localhost:8000/admin/.
That's it.
Why it exists
Reflex sends UI events over a WebSocket on /_event. Django middleware doesn't run on WebSockets. So request.user, sessions, messages, and CSRF aren't available inside @rx.event handlers by default — and the SPA usually wants its own port, which breaks cookie sharing.
reflex-django builds a synthetic HttpRequest for every event, runs your full settings.MIDDLEWARE chain on it, and binds self.request, self.user, self.session, self.messages, self.csrf_token onto your AppState handler. One process. One port. Same auth as your admin.
Full explanation: Why reflex-django exists.
Three files, three jobs
| File | What you configure |
|---|---|
settings.py |
INSTALLED_APPS, MIDDLEWARE (incl. AsyncStreamingMiddleware), REFLEX_DJANGO_* |
urls.py |
reflex_mount(app_name=..., django_prefix=..., rx_config={...}) |
{app}/views.py |
@template-decorated pages and AppState subclasses |
No rxconfig.py. No {app}/{app}.py. No separate frontend.
Versions
| Version | |
|---|---|
| Python | 3.12+ |
| Django | 6.0+ |
| Reflex | 0.9.2+ |
Documentation
The full docs walk you through the why, the how, and every knob:
- Why reflex-django exists — the one-page story
- How Django works in 5 minutes
- How Reflex works in 5 minutes
- How the two fit together
- Your first app — a 15-minute todo list
- Add to an existing Django project
Site: https://web7ai.github.io/reflex-django/
Common commands
python manage.py run_reflex # dev server (auto-rebuild + watch)
python manage.py run_reflex --skip-rebuild # faster reloads for pure Python edits
python manage.py export_reflex # build the SPA bundle (for CI / deploy)
python manage.py migrate
python manage.py createsuperuser
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 reflex_django-0.4.4.tar.gz.
File metadata
- Download URL: reflex_django-0.4.4.tar.gz
- Upload date:
- Size: 211.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c24f3d70c570c5c5542ddef7381b453fe197d0caf27611fa882bb9289437cea
|
|
| MD5 |
79ce3e86df3ff80939a19ecfe7bf274c
|
|
| BLAKE2b-256 |
66649342e75cd4750e4c7c3fe19696f8702d9294387eca9a6af3f3c6853c1f4a
|
File details
Details for the file reflex_django-0.4.4-py3-none-any.whl.
File metadata
- Download URL: reflex_django-0.4.4-py3-none-any.whl
- Upload date:
- Size: 201.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06c2bf22f77abff7c9fdbc8c3ecb8b6090470322a4342fe4316ea969a10c1e54
|
|
| MD5 |
bd18130f532c46e0f1923ce1dac35094
|
|
| BLAKE2b-256 |
5de0c56682d149d71d430712a146874c4498a2839fb620cf6462871e98fa3194
|