Skip to main content

Run a Django backend (Django ORM, Django Admin) alongside a Reflex app.

Project description

reflex-django

reflex-django

Django + Reflex in one process — configure in urls.py, pages in views.py.

PyPI Python Docs License

Documentation · GitHub · PyPI


What it does

  • Django/admin, /api, ORM, migrations, sessions
  • Reflex — SPA UI, client routes, WebSocket events
  • One commandpython manage.py run_reflex
  • No myapp/myapp.py — pages in myapp/views.py, app loaded via django_led_app

Reflex settings go in reflex_mount() in urls.py, not in a large rxconfig.py.


Minimal setup (copy-paste)

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",  # your app
]

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",  # ASGI streaming
]

ROOT_URLCONF = "config.urls"

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",),
        rx_config={"frontend_port": 3000, "backend_port": 8000},
    ),
]

shop/views.py — pages + AppState

import reflex as rx
from reflex_django import template
from reflex_django.state import AppState


class HomeState(AppState):
    message: str = "Hello"

    @rx.event
    async def on_load(self):
        if self.request.user.is_authenticated:
            self.message = f"Hello, {self.request.user.get_username()}"
        else:
            self.message = "Hello, guest"


@template(route="/", title="Home")
def index() -> rx.Component:
    return rx.vstack(
        rx.heading("Home"),
        rx.text(HomeState.message),
    )

Run

python manage.py migrate
python manage.py run_reflex

Open http://localhost:3000/ — admin at http://localhost:3000/admin/

Full walkthrough: Quickstart


Three files to remember

File You configure
settings.py INSTALLED_APPS, MIDDLEWARE (incl. AsyncStreamingMiddleware)
urls.py reflex_mount(app_name=..., django_prefix=..., rx_config={...})
{app}/views.py @template(route=...) pages and AppState subclasses

AppState in one minute

Subclass AppState when a page needs the Django user or session:

from reflex_django.state import AppState

class MyState(AppState):
    @rx.event
    async def on_load(self):
        user = self.request.user          # Django User
        if user.is_authenticated:
            ...
        self.request.session["key"] = "value"
        await self.request.session.asave()

The event bridge attaches self.request on every WebSocket event (same cookies as /admin/).


Why reflex-django?

Reflex uses WebSockets for UI events — Django middleware does not run there by default. reflex-django adds an event bridge so self.request.user and sessions work in @rx.event handlers.

Django Python
6.0.x 3.12+

Documentation

Topic Link
Quickstart (step-by-step) quickstart.md
reflex_mount() reference configuration.md
AsyncStreamingMiddleware async_streaming_middleware.md
Pages in views.py pages_in_views.md
URLs & django_led_app django_urls.md
Brownfield Django existing_django_project.md
Auth & permissions authentication.md

Site: https://mohannadirshedat.github.io/reflex-django/


Commands

python manage.py run_reflex      # dev server
python manage.py migrate
python manage.py createsuperuser

Author: Mohannad Irshedat · GitHub

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

reflex_django-0.4.0.tar.gz (167.0 kB view details)

Uploaded Source

Built Distribution

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

reflex_django-0.4.0-py3-none-any.whl (156.5 kB view details)

Uploaded Python 3

File details

Details for the file reflex_django-0.4.0.tar.gz.

File metadata

  • Download URL: reflex_django-0.4.0.tar.gz
  • Upload date:
  • Size: 167.0 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

Hashes for reflex_django-0.4.0.tar.gz
Algorithm Hash digest
SHA256 2f6338fc0296e33672244e32e1dafbfb29038de3b0c36229a1d1177e009dbd63
MD5 d61d70899259a6b8e947c5c2e0dafaea
BLAKE2b-256 ebf5bbf68aa5eb78f6968da9457ce505794f3e5a80e2a9fa9ee034826f04b382

See more details on using hashes here.

File details

Details for the file reflex_django-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: reflex_django-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 156.5 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

Hashes for reflex_django-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de3377e8018c7fdfc37c624d6b1240c2ff05b42ab41ec0a26142bc905afe2919
MD5 b11ce41913afdc0dcd04a762e849698c
BLAKE2b-256 13987e4b588442fbfe1b8f9e9b6de9b41930c08298c36ca3fa5470881ee2056e

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