Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding React inside a Django project

Tags:

I am a Django developer just getting started with adding React to one page of my app, and really enjoying it so far. (It's a normal Django app with a home page, an about page, etc, but also a "chart" page with an interactive chart, and I want to build the interactive part in React.)

The problem is that I've started with the downloadable React starter kit and I'm not sure how to do things the 'right' way, and it's complicated by using Django to serve my project (all the tutorials seem to assume you're using node, which I'm not).

Right now I just have this in my Django template:

<div id="myapp"></div> <script src="/static/js/vendor/react.js"></script> <script src="/static/js/vendor/JSXTransform.js"></script> <script src="/static/js/myapp.js"></script> 

And myapp.js has all the React code. I'm aware this isn't really the grown-up modern JS way of doing things.

Now I want to use React Bootstrap, but it seems that the only sensible way to do that is with npm. So it's time to make the switch, but I'm not completely sure how.

I have run npm install react and npm install react-bootstrap from inside my static/js directory in Django. This has created a node_modules folder with various files inside.

So three questions from a confused newbie:

  1. Where should I put my React code to work with these npm modules (should I use var React = require('react')?
  2. Do I need to compile this code somehow (using webpack?)
  3. How do I then integrate this with Django? Should I compile it all to myapp.js and just include that in my HTML template?
like image 731
Richard Avatar asked Apr 17 '15 16:04

Richard


People also ask

Can I combine Django and React?

We also established that when using React and Django together, React serves as the frontend (client-side framework), that handles the UI and getting & setting data via requests to the Django backend, which is an API built using the Django REST framework (DRF).

Can I use React in Django template?

How a "single-page-app" can be rendered inside a Django template. And then, separately, in our JavaScript codebase, we mount our React app using the "js-framework-home" div id from the template. import React from 'react'; import ReactDOM from "react-dom"; import EmployeeApplication from "./App"; ReactDOM.


Video Answer


1 Answers

I'm also doing the same thing right now - moving away from embedded HTML script tags into require land. Here is the tutorial I am following, and here is my file system so far. I am doing it in Node but it shouldn't be that different for a Django project as the React frontend code is decoupled from any backend other than API URL's.

Your node_modules folder contains react-bootstrap. In your myapp.js, use the require('react-bootstrap') to load up the library which is contained in your node_modules folder.

  1. Where should I put my React code to work with these npm modules (should I use var React = require('react')?

You can put the code anywhere. If your file system looks like this:

project/   react/     myapp.js   node_modules/     react source code     react bootstrap stuff 

Then you can just do var React = require('react'); in myapp.js.

  1. Do I need to compile this code somehow (using webpack?)

Yes, I would consult the webpack tutorial I linked earlier, it should explain how to compile all your React code into a single bundle.js. Here is also another good tutorial. This bundle.js file contains all the source code of your requires. So if your myapp.js looks something like

var React = require('react'); var ReactBootstrap = require('react-bootstrap'); 

then the bundle.js now contains all of the React and react-bootstrap javascript code, along with the myapp.js source code.

  1. How do I then integrate this with Django? Should I compile it all to myapp.js and just include that in my HTML template?

I've only done work on Nodejs, but my React code so far hasn't touched any Node code, and I don't think it will touch any Django code (again I've never done Django so I might be wrong). All you need to do is compile with webpack, which spits out a bundle.js. You put that bundle.js in your HTML and it'll load up myapp.js.

like image 144
Keith Yong Avatar answered Sep 28 '22 08:09

Keith Yong