How to get Django and ReactJS to work together? How to get Django and ReactJS to work together? reactjs reactjs

How to get Django and ReactJS to work together?


I don't have experience with Django but the concepts from front-end to back-end and front-end framework to framework are the same.

  1. React will consume your Django REST API. Front-ends and back-ends aren't connected in any way. React will make HTTP requests to your REST API in order to fetch and set data.
  2. React, with the help of Webpack (module bundler) & Babel (transpiler), will bundle and transpile your Javascript into single or multiple files that will be placed in the entry HTML page. Learn Webpack, Babel, Javascript and React and Redux (a state container). I believe you won't use Django templating but instead allow React to render the front-end.
  3. As this page is rendered, React will consume the API to fetch data so React can render it. Your understanding of HTTP requests, Javascript (ES6), Promises, Middleware and React is essential here.

Here are a few things I've found on the web that should help (based on a quick Google search):

Hope this steers you in the right direction! Good luck! Hopefully others who specialize in Django can add to my response.


I feel your pain as I, too, am starting out to get Django and React.js working together. Did a couple of Django projects, and I think, React.js is a great match for Django. However, it can be intimidating to get started. We are standing on the shoulders of giants here ;)

Here's how I think, it all works together (big picture, please someone correct me if I'm wrong).

  • Django and its database (I prefer Postgres) on one side (backend)
  • Django Rest-framework providing the interface to the outside world (i.e. Mobile Apps and React and such)
  • Reactjs, Nodejs, Webpack, Redux (or maybe MobX?) on the other side (frontend)

Communication between Django and 'the frontend' is done via the Rest framework. Make sure you get your authorization and permissions for the Rest framework in place.

I found a good boiler template for exactly this scenario and it works out of the box. Just follow the readme https://github.com/scottwoodall/django-react-template and once you are done, you have a pretty nice Django Reactjs project running.By no means this is meant for production, but rather as a way for you to dig in and see how things are connected and working!

One tiny change I'd like to suggest is this:Follow the setup instructions BUT before you get to the 2nd step to setup the backend (Django here https://github.com/scottwoodall/django-react-template/blob/master/backend/README.md), change the requirements file for the setup.

You'll find the file in your project at /backend/requirements/common.pipReplace its content with this

appdirs==1.4.0Django==1.10.5django-autofixture==0.12.0django-extensions==1.6.1django-filter==1.0.1djangorestframework==3.5.3psycopg2==2.6.1

this gets you the latest stable version for Django and its Rest framework.

I hope that helps.


As others answered you, if you are creating a new project, you can separate frontend and backend and use any django rest plugin to create rest api for your frontend application. This is in the ideal world.

If you have a project with the django templating already in place, then you must load your react dom render in the page you want to load the application. In my case I had already django-pipeline and I just added the browserify extension. (https://github.com/j0hnsmith/django-pipeline-browserify)

As in the example, I loaded the app using django-pipeline:

PIPELINE = {    # ...    'javascript':{        'browserify': {            'source_filenames' : (                'js/entry-point.browserify.js',            ),            'output_filename': 'js/entry-point.js',        },    }}

Your "entry-point.browserify.js" can be an ES6 file that loads your react app in the template:

import React from 'react';import ReactDOM from 'react-dom';import App from './components/app.js';import "babel-polyfill";import { Provider } from 'react-redux';import { createStore, applyMiddleware } from 'redux';import promise from 'redux-promise';import reducers from './reducers/index.js';const createStoreWithMiddleware = applyMiddleware(  promise)(createStore);ReactDOM.render(  <Provider store={createStoreWithMiddleware(reducers)}>    <App/>  </Provider>  , document.getElementById('my-react-app'));

In your django template, you can now load your app easily:

{% load pipeline %}{% comment %} `browserify` is a PIPELINE key setup in the settings for django  pipeline. See the example above{% endcomment %}{% javascript 'browserify' %}{% comment %} the app will be loaded here thanks to the entry point you created in PIPELINE settings. The key is the `entry-point.browserify.js` responsable to inject with ReactDOM.render() you react app in the div below{% endcomment %}<div id="my-react-app"></div>

The advantage of using django-pipeline is that statics get processed during the collectstatic.