Django-React Integration
Smarter’s web console is a single-page React application,
@smarter/console, that Django serves (ADR-035). Its source,
an npm workspace of the console and the packages it is built from, is a repository of its own,
smarter-sh/smarter-console. Every console page, from the dashboard and the
resource lists to the manifest pages, the Extensions console, the manifest drop zone, the
Prompt Passthrough, the Terminal and the Prompt Engineer’s Workbench, is a route of the
console. Django keeps what it is good at: authentication, sessions, CSRF protection,
permissions, url routing of the backend, static file management and deployment. React handles
everything the browser shows.
The design principle is that the console’s build is an ordinary Django static asset.
Vite.js compiles the console into versioned JavaScript and CSS bundles
and an index.html, and each smarter-console release ships them. This repository pins a
release in smarter-console-version.txt, and make console unpacks it into Django’s static
file hierarchy, smarter/smarter/static/react/, where collectstatic picks it up like any
other file. The console then talks to Django through
two apis: the web console api under /api/web/, which uses the
browser’s Django session, and the public Smarter api under /api/v1/.
Request Lifecycle
The following sequence describes how a console page reaches the browser:
A user requests a console url, e.g.
/workbench/llm-clients/<hashed_id>/prompt/.Django routes the request to the page’s view, a subclass of
ConsoleAppView.The view answers with the console’s built
index.html, whatever the page.The browser downloads the console’s scripts and styles, whose hashed names Vite wrote into
index.html.The console mounts onto
#smarter-console-rootand loads its session fromGET /api/web/session/.The console’s router matches the path, and loads the page’s code, a chunk of its own.
The page builds its component’s props from the session and the route’s parameters.
The component calls the backend’s apis under
/api/web/and/api/v1/.
Once the console has loaded, moving between its pages doesn’t ask Django for a page at all: the router renders the next route in place, and only the apis are called.
The Single-Page Console
Django Serves the Console
Each console page keeps a Django url pattern and its url name, so that emails, settings,
templates and reverse() calls that name a page keep working. The page’s view is
ConsoleAppView, or a one-line subclass of it
that exists only to keep its name, such as
PromptWorkbenchView or a
resource’s <Kind>ListView. ConsoleAppView answers GET and HEAD with the console’s
index.html, read from STATIC_ROOT/react/@smarter/console/ on each request, and:
is never cached, so that a deployment’s new build takes effect at once. The scripts and styles that
index.htmlnames have hashed names and are cached as other static files are.sets the CSRF cookie, which the console’s first POST needs.
answers 503 The web console hasn’t been built when
index.htmlis missing, and logs an error.make consoleinstalls the console, andmake collectstaticcollects it.
Because Django serves the console from its own origin, the console’s requests carry the browser’s Django session cookie and CSRF token like any form post would, and no token needs to be handed to React.
The Router and Lazy Routes
The console’s entry point, src/main.tsx, creates a browser router from the routes in
src/router.tsx and mounts the app. The sign-in, sign-up and password reset pages are routes
of their own; every other page is a child of a RequireAuth layout, which sends a visitor who
isn’t signed in to the sign-in page, and draws the console’s frame: the header, the sidebar and
the user menu.
Nearly every page loads lazily, with React Router’s lazy, so that its code is a chunk of its
own that the browser downloads the first time the page is opened. The resource lists
(lib/lists) and the manifest pages (lib/manifestPages) are tables of routes, one entry
per kind, each at the path that Django serves:
// the Prompt Engineer's Workbench, with Smarter Chat and the Console's editor, in a chunk of its own.
{
path: WORKBENCH_PATH,
lazy: async () => ({ Component: (await import("@/routes/WorkbenchPage")).WorkbenchPage }),
},
Import-Only Packages
The React code of a page lives in a package of smarter-console’s npm workspace, in
packages/, e.g. @smarter/workbench, @smarter/guardrail-list or @smarter/terminal-emulator. These
packages are import-only: they have no build of their own, and the console’s build bundles
them. A package becomes import-only when:
its source uses relative imports only, so that another package’s build can compile it.
src/index.tsexports its component, and the types of its props.its
package.jsonpoints"main"and"exports"at./src/index.ts, and exports./mocks, its MSW handlers, for the console’s stories and tests.it has no
buildscript. Itsmain.tsx,index.htmlandvite.config.tsremain for its development page only (npm run dev).
The page’s component receives everything it needs as props. The console’s route builds them, rather than Django rendering them into a template.
Props From the Session
GET /api/web/session/
(ConsoleSessionView) tells the console who is using
it and how the platform is set up: the user, their profile and account, the cookie names and
domain, the sidebar’s links, the onboarding state, and the feature flags, such as
reactDebugMode, serverLogs or terminal. Its contract is the Pydantic model
ConsoleSession, from which the console’s
TypeScript types (src/contract.gen.ts) are generated. SessionProvider loads it once, when
the console starts, and pages read it with useConsoleSession().
A page turns the session into its component’s props. lib/djangoSession.ts builds the
SessionContext of @smarter/common that every package’s api calls use, with the CSRF and
session cookie names, the cookie domain, the debug flag and a new request id. A flag that is off
hides its page: the tool pages (routes/ToolPages) show Page not found while the session’s
manifestDropZone, passthrough or terminal is off.
An Example: The Prompt Engineer’s Workbench
The LLMClient prompt engineering workbench is a good example. It is a highly interactive application, with streaming chat responses, live server logs, a manifest editor and complex state, and every layer of the integration shows in it.
Django. A url pattern routes /workbench/llm-clients/<hashed_id>/prompt/ to
PromptWorkbenchView, a
ConsoleAppView. It answers with the console’s index.html, like every other page, and it
needs no template and no context.
The route. The console’s router matches WORKBENCH_PATH,
/workbench/llm-clients/:id/prompt, and loads src/routes/WorkbenchPage.tsx. The page first
asks the resource api, /api/web/dashboard/resource/llmclient-<hashed_id>/, whether the user
may read the LLMClient. On a 404, for an unknown LLMClient, another account’s, or a staff-only
one, it shows Page not found, by the same rule as the console’s manifest pages.
The configuration. src/lib/workbench.ts builds the workbench’s WorkbenchConfig from
the hashed id and the session: the chat’s apiUrl, the LLMClient in the public api,
/api/v1/llm-clients/<hashed_id>/, from whose config/ the chat reads its configuration;
the Manifest, Architecture and Deployment tabs’ urls in the web console api,
/api/web/prompt/llm-clients/<hashed_id>/; the apply, validate and delete apis; the Server Logs
stream, while the session’s serverLogs is on; the cookie names; and the router’s navigate,
with which the workbench returns to the LLM Clients list after its LLMClient is deleted.
The package. @smarter/workbench renders the chat, Smarter Chat
(the @smarter.sh/ui-chat npm package), and the developer Console beside it. From its own
perspective, the integration boundary is one prop: it neither knows how its code was built and
served, nor which Django view answered the url.
Note
No console page references a static asset’s file name, and no Django template names a
console page’s script. Vite writes the hashed names into index.html and into the chunks
that load each other.
The Template-Tag Pattern of a Deployed LLMClient’s Page
One page is not part of the console, on purpose: the page that a browser sees at a deployed
LLMClient’s own url, e.g. https://<name>.<account>.<api domain>/. That host serves one
LLMClient and its api endpoints, /config/ and /prompt/, and nothing else; the console
isn’t there, and a visitor has no session on it. A GET or HEAD to it receives a page about
the LLMClient instead, while the enable_llmclient_endpoints_page waffle switch is active: its
url, the curl commands that call it, the ways to put its chat on a web site, and its manifest.
Django renders that page from a template, and mounts its React app, @smarter/llmclient-deployment,
with a template tag. It is the only page that does so. The Prompt Engineer’s Workbench shows the
same content in its Deployment tab, by importing the package’s component.
Important
A new page is a console page: an import-only package and a route of @smarter/console,
served by a ConsoleAppView. Never add a new Django template for a page.
An Example: The Deployed LLMClient’s Page
LLMClientEndpointsView finds the LLMClient that
serves the request’s host, and builds the page’s data with
deployment_page(), a Pydantic model,
LLMClientDeploymentPage, whose public
copy names no owner and has no api keys. It renders the template
smarter/templates/llmclient/endpoints.html, which loads
smarter.apps.llmclient.templatetags.react_llmclient_deployment. The tag module does two
things:
llmclient_deployment_react_assetsreturns the app’s JavaScript and CSS files, from itsmanifest.json.DEPLOYMENT_ROOT_IDandDEPLOYMENT_DATA_IDname the app’s root element and thejson_scriptelement that holds its data, so that the view, the template and the app agree on them.
The template writes the data with Django’s json_script filter, and renders the root element,
which names the data’s element in a custom HTML attribute:
<script id="smarter-llmclient-deployment-data" type="application/json">{"mode": "public", ...}</script>
<div
id="smarter-llmclient-deployment-root"
class="d-flex flex-column w-100"
smarter-deployment-data-id="smarter-llmclient-deployment-data"
react-debug-mode="False"
>
</div>
and the <link> and <script> elements of the app’s assets:
{% llmclient_deployment_react_assets as assets %}
{% for js_file in assets.js %}
<script class="smarter" type="module" src="{% static 'react/@smarter/llmclient-deployment/' %}{{ js_file }}"></script>
{% endfor %}
The package’s main.tsx finds the root element, reads the data that its attribute names, and
renders the app, with no session: the page has no actions. Unlike a console page, the package
has a build script of its own, whose app each smarter-console release ships, and make console
installs into static/react/@smarter/llmclient-deployment/. Its main and exports still
point at src/index.ts, so that the workbench can import its component.
React Build Manifest
An app’s script names cannot be written into a template, because Vite gives every production
file a content-based hash for browser cache invalidation. Instead, the app’s build writes a
manifest.json file, the authoritative list of the JavaScript and CSS files it produced, and
Django reads it at render time. Its exact contents vary with the application’s dependencies,
vendor libraries and code-split modules. The following example shows the manifest of an
application that imports xterm.js:
{
"_rolldown-runtime-B8sk0Y4v.js": {
"file": "assets/rolldown-runtime-B8sk0Y4v.js",
"name": "rolldown-runtime"
},
"_xterm-BVTBumqj.js": {
"file": "assets/xterm-BVTBumqj.js",
"name": "xterm",
"imports": [
"_rolldown-runtime-B8sk0Y4v.js"
],
"css": [
"assets/xterm-kHJ-D0s7.css"
]
},
"_xterm-kHJ-D0s7.css": {
"file": "assets/xterm-kHJ-D0s7.css",
"src": "_xterm-kHJ-D0s7.css"
},
"index.html": {
"file": "assets/index-B1eOzN5c.js",
"name": "index",
"src": "index.html",
"isEntry": true,
"imports": [
"_rolldown-runtime-B8sk0Y4v.js",
"_xterm-BVTBumqj.js"
],
"css": [
"assets/index-58MXwt-L.css"
]
},
"_custom": {
"buildTime": "2026-10-12T01:09:47.988Z",
"version": "0.3.0",
"buildEnv": "production"
}
}
Every entry except _custom is a chunk generated by Vite. _custom is build information
that the app’s Vite configuration adds after the build, with its addCustomManifestData
plugin: the build time, the package’s version and the build’s NODE_ENV.
One entry, marked "isEntry": true, is the application’s entry point and the root of the
dependency graph. Rather than loading only the entry point’s file, Django traverses the manifest
recursively to find every file the application needs: imported chunks, shared runtime modules,
vendor bundles and their CSS.
Django doesn’t read the manifest as loose JSON. It validates the file against the Pydantic model
ViteManifest: each chunk is a
ViteManifestChunk, and
_custom is a
ReactAppBuildInfo. Chunks accept
fields that Vite or Rolldown add in newer versions, so a Vite upgrade doesn’t break page
rendering. _custom is Smarter’s own shape and accepts only its three fields. A manifest that
is missing, isn’t JSON, or doesn’t match the model is logged as an error and treated as absent,
and the page then renders without the app’s assets.
Vite Configuration
Vite.js compiles the React code, optimizes the bundles, and writes them where Django serves them. Rather than requiring React code to understand deployment environments, static asset locations or runtime integration, these concerns are centralized in the build configuration. The console’s configuration, packages/smarter-console/vite.config.ts, addresses these requirements:
- Static Asset Integration
Writes
index.htmland its assets intodist/react/@smarter/console/, the layout of Django’s static file hierarchy, with the base url/static/react/@smarter/console/, so that, oncemake consoleinstalls them intosmarter/smarter/static/react/, they take part in thecollectstaticworkflow andConsoleAppViewfindsindex.htmlinSTATIC_ROOT.
- Hashed File Names
Names every script, chunk and stylesheet
assets/[name]-[hash], so that browsers can cache them for as long as they like.
- Code Splitting
Writes each lazy route, and the import-only packages it uses, into a chunk of its own.
- Development Proxying
Serves the console at
http://localhost:5173/withnpm run dev, and proxies every path that belongs to the backend (/api,/admin,/social-auth,/i18n,/jsi18n,/staticand the/wsWebSocket) to the Django dev server athttp://localhost:9357, so that the browser sees one origin and Django’s session and CSRF cookies work as they do in production.
- Production Hardening
Removes
console.debug()calls from production builds, with the Oxc minifier, so that debugging output can’t disclose sensitive information.
export default defineConfig(({ command }: ConfigEnv) => ({
plugins: [react()],
base: command === "serve" ? "/" : `/static/react/${packageName}/`,
resolve: {
alias: {
"@": path.resolve(import.meta.dirname, "./src"),
},
},
build: {
manifest: "manifest.json",
outDir: `../../dist/react/${packageName}`,
emptyOutDir: true,
rolldownOptions: {
output: {
entryFileNames: "assets/[name]-[hash].js",
chunkFileNames: "assets/[name]-[hash].js",
assetFileNames: "assets/[name]-[hash][extname]",
// The Oxc minifier's way of removing console.debug() calls, which can log sensitive data.
minify: {
compress: {
treeshake: {
manualPureFunctions: ["console.debug"],
},
},
},
},
},
},
server: {
port: 5173,
strictPort: true,
proxy: backendProxy(),
},
}));
An import-only package’s vite.config.ts, such as
packages/smarter-workbench/vite.config.ts,
is for its development page only: it
has no build section, and proxies /api and /static to the Django dev server.
@smarter/llmclient-deployment’s configuration also writes a manifest.json, with the
_custom build information that its template tag validates.
Build, Deployment, and CI/CD Considerations
The React code is a single npm workspace, smarter-console, with one package per page or
widget. make build in that repository builds, in order, the libraries that others import
(@smarter/common, @smarter/didyouknow’s and @smarter/onboarding’s libraries,
@smarter.sh/ui-widgets and @smarter.sh/ui-chat), then every package that has a build
script. Besides those libraries (@smarter.sh/ui-chat’s build adds its embed script), only
two packages have one, and both are apps, each built into its own folder of dist/react/, the
layout of Django’s static file hierarchy: the console, which bundles every page package and the packages of its frame (the
user menu, Did you know?, onboarding and the welcome dialog), and the deployed LLMClient’s
page:
static/react/@smarter/console/
static/react/@smarter/llmclient-deployment/
Note
React build artifacts are treated as ordinary Django static assets.
From Django’s perspective, compiled React bundles are no different from any other static resource such as CSS, images, or JavaScript files. Once generated, they participate in the standard Django static asset workflow, including collectstatic, static file serving, and cache management.
Because React assets are generated before Django is deployed, the frontend build remains
cleanly separated from the Django runtime. The console is developed, versioned, tested and
released in its own repository, and the backend installs a pinned release of it: each
release attaches smarter-console-<version>.tar.gz, with react/, the builds, and
i18n/messages.js, the console’s translatable messages (see Translations).
CORS
@smarter/common sends a set of custom HTTP headers with every api request (see
packages/smarter-common/src/lib/django.tsx). They are listed in
CORS_ALLOW_HEADERS in smarter.settings.base, so that a cross-origin request, e.g. from the
embedded Smarter Chat, may carry them. CORS is switch-enabled: the enable_middleware_cors
waffle switch must be on for a React app on another origin to call the backend.
const requestHeaders = {
Accept: applicationJson,
"X-CSRFToken": csrftokenValue!,
"Content-Type": applicationJson,
"X-Smarter-Client": sessionContext.smarterClient,
"X-Smarter-ClientVersion": sessionContext.smarterClientVersion,
"X-Smarter-ClientType": "react",
"X-Smarter-RequestId": sessionContext.smarterRequestId,
"X-Smarter-Capabilities": capabilities,
};
Operational Considerations
- Source Control Exclusion
Compiled React build artifacts are intentionally excluded from the Git repository and are never committed to source control.
make consoleinstalls them from the pinned release, and replacessmarter/smarter/static/react/each time.
- Build Prerequisites
Django can serve no console page until the console has been built and collected: until then every page answers 503. The deployed LLMClient’s page, likewise, depends on its app’s
manifest.json.
- Upgrading the Console
Change the version in
smarter-console-version.txt, thenmake console,make collectstaticandmake build.make buildalone uses the static files collected earlier.
- Trying an Unreleased Console Change
Run
make buildin a smarter-console checkout, thenmake console-local CONSOLE_DIST=../smarter-console/disthere, which installs that build instead of the pinned release.
- CI/CD Pipeline Initialization
GitHub Actions workflows begin with a clean checkout that contains no compiled frontend assets. Accordingly, the build (
build.yml) and the Python tests (test.yml) install the pinned release first, withscripts/fetch_console.py, before Docker builds, collectstatic, integration tests, or deployment stages that depend on it. A failed download fails the build.
- Static File Synchronization
During local development, be aware of the distinction between Vite’s development server, Django’s static asset directories, and the Django staticfiles runtime directory. Stale build artifacts can lead to confusing runtime behavior if these become out of sync.
- Container Build Dependencies
Docker images that serve the console must be built only after the frontend build has completed, so that
index.html, the compiled bundles and the deployed LLMClient’s page’smanifest.jsonare inside the image at runtime.
- Published Packages
Smarter Chat, the chat of the Prompt Engineer’s Workbench, is also published to npm, as
@smarter.sh/ui-chat, and so is@smarter.sh/ui-widgets. Both are packages of smarter-console (packages/smarter.sh-chatandpackages/smarter.sh-widgets) that build on their own, as libraries that the console’s pages import, and its GitHub Actions publish them when a smarter-console release changes them, under the console’s version (uiChatRelease.ymlanduiWidgetsRelease.yml).
Collectively, these conventions give a predictable, reproducible deployment model that works the same way in local development, CI/CD workflows, Docker image builds and production. The result keeps the operational simplicity of a Django deployment while the web console is a modern single-page application.
Technical Reference
- Dashboard React Component
- User Menu React Component
- Did You Know? React Component
- Doodle React Component
- Onboarding Hints React Component
- LLMClient List React Component
- Apply Manifest React Component
- Prompt Passthrough React Component
- Smarter Chat
- Terminal Emulator React Component
- Welcome Dialog React Component
- SmarterReactTemplateTagManager