Skip to main content

Django React Bridge

Think at Create React App but for Django

Installation

pip install django-react-bridge

Configuration

Add react_bridge to the installed apps:

INSTALLED_APPS = (
    # ...
    'react_bridge',
)

Add the react components context processor:

TEMPLATES = [
    {
        # ...
        'OPTIONS': {
            'context_processors': [
                # ...
                'react_bridge.context_processors.react_components',
            ],
        },
    },
]

Configure react bridge:

REACT_BRIDGE_JS_CONFIG = {
    # Should use yarn or npm? default: True
    'use_yarn': True,

    # Path of your npm_command or yarn command according to use_yarn default: yarn
    'npm_command': '/bin/path/to/my/yarn/or/npm',

    # Path of package json where js is installed default: None your should configure your own
    'package_json_path': os.path.join(BASE_DIR, 'js'),

    # Path of bundler output default: package_json_path + '/static/build'
    'output_path': os.path.join(BASE_DIR, 'static/build'),

    # Url where the bundler output is server default: '/static/build/'
    'output_url': '/static/build/',

    # Webpack entry: https://webpack.js.org/configuration/entry-context/#entry
    # default: package_json_path + '/src/index.js'
    'entry': os.path.join(BASE_DIR, 'js/src/index.js'),

    # Webpack resolve: https://webpack.js.org/configuration/resolve/#resolve
    # default: None
    'resolve': {
      'alias': {
        '@components': os.path.join(BASE_DIR, 'shared/components'),
      }
    },

}

# Use js bridge in dev mode
REACT_BRIDGE_DEV = True

To use django-react-bridge in non standard paths you should include your top static in your STATICFILES_DIRS or your can change output_path according to your staticfile configuration.

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

To install al required node packages needs to run django-react-bridge run:

python manage.py jsinit path/to/my/js/project

This commands install all for you and when finished print on console a base REACT_BRIDGE_JS_CONFIG with adjusted path related to initial given project path.

Usage

Inside your entry javascript file:

import React from 'react'
import ReactDOM from 'react-dom'
import { registerComponents } from 'django-react-bridge'
import MyComponent from './MyComponent'

registerComponents({
  React,
  ReactDOM,
  components: {
    // Register here yuor components
    MyComponent,
  }
})

Inside your Django template:

{% load react_bridge %}
<html>
  <head>
    {% react_head_tags %}
  </head>
  <body>

    {% render_component "MyComponent" in_tag='div' myPropA='a' myPropB='b' %}

    {% react_body_tags %}
  </body>
</html>

Multi bundle set up:

If you need to split your app in multiple bundles:

REACT_BRIDGE_JS_CONFIG = {
    'entry': {
      'frontend': os.path.join(BASE_DIR, 'front/src/index.js'),
      'admin': os.path.join(BASE_DIR, 'admin/src/index.js'),
    }
}

Then in your template you need to pass the bundle name when render related tags:

{% load react_bridge %}
<html>
  <head>
    {% react_head_tags 'admin' %}
  </head>
  <body>
    {% react_body_tags 'admin %}
  </body>
</html>

Commands

NOTE:

Before run these commands you should run jsinit to install al relative packages needed to run js build tools.

jsdevserver

Start dev server on port 9000.

You should have REACT_BRIDGE_DEV = True.

python manage.py jsdevserver

jsbuild

Build your js and realted assets according to your REACT_BRIDGE_JS_CONFIG.

python manage.py jsbuild

jsinit

Init a js project at given path.

python manage.py jsinit path/to/my/js/project

Metadata

Release files for django-react-bridge 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-react-bridge 0.2.0
File Size Uploaded Provenance Actions
django-react-bridge-0.2.0.tar.gz 8.4 kB Without provenance Details for django-react-bridge-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-react-bridge 0.2.0
File Interpreter ABI Platform Actions
django_react_bridge-0.2.0-py3-none-any.whl Python 3 none any Details for django_react_bridge-0.2.0-py3-none-any.whl

Total release size: 22.1 kB

Release files / django-react-bridge-0.2.0.tar.gz

Download URL django-react-bridge-0.2.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
380611ffca87ce8c9239e0c88947b0f48263789be0ddf5e5ac20e93cb0a51095
BLAKE2b-256 checksum
How to use checksums
0051c6eacc5496137268cb471f240bb863f794ca0075d05a23cd1137f059b82e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.5

Release files / django_react_bridge-0.2.0-py3-none-any.whl

Download URL django_react_bridge-0.2.0-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4f678657a46d6e1e1d19022d3c3be4a1daca3e3766226f72acfa09ba18bb652
BLAKE2b-256 checksum
How to use checksums
ed657199e0ad98af0fd73bc2d108dcf7eaf168b9862fcc6b1c0490ce000ebc30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.5

Security for django-react-bridge 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page