New to Django and even newer to ReactJS. I have been looking into AngularJS and ReactJS, but decided on ReactJS. It seemed like it was edging out AngularJS as far as popular
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(
, 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 %}
The advantage of using django-pipeline is that statics get processed during the collectstatic
.