Django-React Integration ======================== Smarter integrates `React `__ applications directly into Django's server-rendered architecture rather than treating React as a separate single-page application. This approach preserves Django's strengths in authentication, session management, template rendering, routing, and deployment while enabling modern, highly interactive user interfaces. The core design principle is that React build artifacts are treated as ordinary Django static assets that leverage Django's existing serving and management of authentication and session data. React applications are compiled by `Vite.js `__ into versioned JavaScript and CSS bundles and emitted directly into Django's static file hierarchy. At runtime, Django discovers and loads these assets through a manifest-driven integration layer that automatically resolves hashed filenames, shared chunks, vendor bundles, and code-split dependencies. As a result, Django remains responsible for request routing, authentication, template rendering, runtime configuration, static asset management, and deployment orchestration, while React remains focused exclusively on frontend behavior and presentation logic. Frontend and backend development can therefore proceed largely independently while still participating in a unified deployment model. .. image:: https://cdn.smarter.sh/docs/smarter-framework/react-integration/react-integration.002.png :alt: View, Template, and React Component Integration :width: 100% Request Lifecycle ----------------- The following sequence describes how a React application is rendered within the Django runtime: 1. A user requests a Django URL. 2. Django routes the request to a view. 3. The view generates runtime configuration and template context. 4. A custom template tag loads and analyzes the React build's manifest.json file. 5. JavaScript and CSS dependencies are recursively discovered. 6. Django renders the template and injects the required asset references. 7. The browser downloads the React bundles. 8. React IIFE (Immediately Invoked Function Expression) mounts onto a designated DOM element. 9. React reads runtime configuration from HTML attributes. 10. React begins communicating with backend APIs. This architecture creates a lightweight integration boundary between Django and React without requiring hardcoded asset references, inline bootstrap scripts, environment-specific templates, or additional initialization APIs. The remainder of this guide follows a complete implementation example and then examines each integration layer in detail, including manifest analysis, template tags, Vite configuration, runtime context propagation, and deployment considerations. An Example: The Terminal Application Component ------------------------------------------------ Smarter's live view of server log activity is a great example, in that it is a highly interactive React component that involves live streaming data, complex state management, and tight integration with Django's authentication and server-side context management. Functionality of this nature could only be implemented in a frontend framework like React. .. image:: https://cdn.smarter.sh/docs/smarter-framework/react-integration/react-integration.001.png :alt: View, Template, and React Component Integration :width: 100% Django Objects ~~~~~~~~~~~~~~~~~ The Terminal Application begins with a standard Django request lifecycle. A URL pattern routes incoming requests for the pattern `/dashboard/logs/` to the Django view class :class:`TerminalEmulatorLogView`. The view is also responsible for generating and providing the API endpoint that the React application will use to retrieve log data. Rather than hardcoding endpoint URLs, the view uses Django's :func:`reverse()` function and Django URL namespaces to generate the correct endpoint at runtime. This ensures that routing remains centralized within Django, that it remains future-proofed against code refactoring, and that it assists code contributors in understanding how frontend and backend routing are connected without duplicating URL definitions across templates and React components. .. image:: https://cdn.smarter.sh/docs/smarter-framework/react-integration/react-integration.003.png :alt: View, Template, and React Component Integration :width: 100% The view's primary responsibilities are straightforward: * Select the template to render. * Generate runtime configuration required by the frontend. * Provide template context. The template then renders the initial HTML response and provides a DOM mounting point for the React application. .. image:: https://cdn.smarter.sh/docs/smarter-framework/react-integration/react-integration.004.png :alt: View, Template, and React Component Integration :width: 100% In addition to rendering the mounting element, the template performs two important integration tasks. First, it serializes runtime configuration generated by the view into custom HTML attributes attached to the mounting element (see example below). These attributes become the transport mechanism through which Django passes configuration data into React during application initialization. .. code-block:: html
.. note:: In this integration scheme, React does not technically require session nor csrf data in order to function, since React is served by Django and therefore shares the same origin. However, we pass these values in the interest of future-proofing against the possibility of serving React from a different origin, which would require explicit handling of authentication and CSRF tokens in API requests. Second, the template invokes a custom Django template tag that analyzes the React build's manifest.json file and injects the required {% endfor %} {% endblock %} From the template’s perspective, the interaction is intentionally simple. The template requests a collection of CSS and JavaScript assets and renders them. The details of manifest loading, dependency traversal, asset ordering, and filename resolution remain encapsulated within the template tag implementation. See :doc:`SmarterReactTemplateTagManager `. .. image:: https://cdn.smarter.sh/docs/smarter-framework/react-integration/react-integration.008.png :alt: View, Template, and React Component Integration :width: 100% .. image:: https://cdn.smarter.sh/docs/smarter-framework/react-integration/react-integration.010.png :alt: View, Template, and React Component Integration :width: 100% Django Template ~~~~~~~~~~~~~~~~~~ The Django template for the Terminal Application serves as the final integration point between Django and React. It is responsible for assembling the HTML required to initialize the React application and making server-side configuration available to the frontend runtime. Specifically, the template performs three functions: 1. Provide a DOM mounting point for the React application. 2. Inject the JavaScript and CSS assets required by the React build. 3. Transport runtime configuration generated by the Django view into React. The JavaScript and CSS assets are generated by the custom template tags described in the previous section. These tags analyze the React build's ``manifest.json`` file and produce the ``