I have created a working React + Django REST project and it works fine on my development machine. I have used the following method to make it work on my CentOS 7 server.
I created the build directory using
npm run buildand copied that to the Django project root folder.I added the
builddirectory on TEMPLATES list in Django settings to identify theindex.htmlfile.I added
build/staticfolder inSTATICFILES_DIRS.I added
url(r'^.*', TemplateView.as_view(template_name='index.html')),line to the rootURLSfile to capture all url patterns and load the index file in build folder that contains the React app.I run
manage.py collectstaticto create astaticfilesfolder with all static files.I added the
staticfilesfolder to the Nginx conf file like following:location /static/ { root /home/michel/project/staticfiles; }I have restarted the nginx server.
I am using the Django server to load the index.html file and I expect that the staticfiles folder will contain necessary static files to load my React app.
However, when I visit www.mydomain.com it loads the index.html file, but does NOT load the React app on <div id='root'></div>. I know this because the footer of the index is shown, but the css for styling that footer is also not working.
I am guessing that I have a problem of making the static files being detected. Any solution?
EDIT Here is the code that links my React app to the index.html file.
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import registerServiceWorker from './registerServiceWorker'; ReactDOM.render(<App />, document.getElementById('root')); registerServiceWorker(); P.S. It is automatically generated by the create-react-app command.
<head>tag and codes contained within it.create-react-appto create my frontend. So the default code to load the app onto a div with idrootis used to load my app to that div. Mind it, when I run the server withgunicorn bind ...for checking Gunicorn, the app works perfectly fine.