Combining both Django and React static files and compiling them.
Now that the code for collecting and accessing the static files has been created, we can compile all of the static files from both the Django admin panel and the React files into the staticfiles folder for deployment.
Collect the admin and DRF staticfiles to the empty staticfiles directory you created earlier, with the following command in the terminal
python3 manage.py collectstatic
Next, we will compile the React application and move its files to the staticfiles folder. In another terminal, cd into the frontend directory
cd frontend
Then run the command to compile and move the React files
npm run build && mv build ../staticfiles/.
You will need to re-run this command any time you want to deploy changes to the static files in your project, including the React code. To do this, you need to delete the existing build folder and rebuild it.
This command will delete the old folder and replace it with the new one:
npm run build && rm -rf ../staticfiles/build && mv build ../staticfiles/.
Now your staticfiles folder should be filled with all the static files needed for deployment
Depending on your specific dependency versions, your file structure in the staticfiles folder may be slightly different from the above image. The folders we need to be sure are there are the admin and build folders.
This will ensure Heroku uses the correct version of Python to deploy your project.
In the root directory of your project, create a new file named runtime.txt
Inside the runtime.txt, add the following line:
python-3.9.16
Everything is ready to go, so let’s see if it works.