Examples of integrating Django w/ React
Starting inside a VM built with Vagrant and populated by Git, install Python/Dango, Node/React, and NGINX to get a functioning demo that started with but has moved past the DigitalOcean Tutorial.
- Git
- Vagrant
- A code editor
- A browser
- cd to an appropriate working directory
git clone https://github.com/Ecotrust/djreact.git
cd djreact
- Review/edit/update the Vagrantfile to match your environment:
- Are you an a Mac with M2 or M3 chips?
- You likely are using QEMU instead of VirtualBox and probably will need a different VM image than
ubuntu/jammy64
- You likely need to use SMB for your shared directories
- You likely are using QEMU instead of VirtualBox and probably will need a different VM image than
- Is your working directory
/usr/local/apps/djreact
? If not, update your shared directories' locations
- Are you an a Mac with M2 or M3 chips?
vagrant up
vagrant ssh
- Follow this tutorial from DigitalOcean to set up Python 3
- Stop after step 2
- For the current code, there may be assumptions that the virtual environment lives at
/usr/local/apps/env
(notenvironments
ormy_env
). It's probable that this isn't important - Inside a VM with shared directories, it is helpful to make sure your env is NOT inside of the shared directory, as permission issues can occur
- Once your virtualenv is active, install the required libraries:
pip install -r /usr/local/apps/djreact/django-todo-react/backend/requirements.txt
python /usr/local/apps/djreact/django-todo-react/backend/manage.py migrate
python /usr/local/apps/djreact/django-todo-react/backend/manage.py runserver 0:8000
- Leave this terminal open and running: do the rest in a new terminal
- Be sure to use
vagrant ssh
again to ensure you're running on your VM rather than your host
- Be sure to use
- Follow this tutorial from DigitalOcean to set up Node.js using the 'NVM' approach:
- run
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install lts/iron
- run
npm install bootstrap reactstrap axios
cd /usr/local/apps/djreact/django-todo-react/frontend
npm run build
sudo apt install nginx
sudo cp /usr/local/apps/djreact/deploy/todo.nginx /etc/nginx/sites-available/todo
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/todo /etc/nginx/sites-enabled/
sudo systemctl start nginx
<-- or mayberestart
- open "http://localhost:8080" on your host machine -> do you get the React TODO app?
- open "http://localhost:8000" on your host machine -> do you get the Django template wrapped around the React TODO app?