This repository serves as a starting point for developing a production-ready application using Django Rest Framework, React with Typescript and Postgres in a Dockerized environment with an option to deploy to Heroku. Running development setup without docker-compose is also possible.
This setup has been tested with Python 3.9 and Node 14. (previously Python 3.8, Node 12)
- Django + Django Rest Framework
djangodjangorestframework django-cors-headers- handling cross origin requestscoverage- for code coverage reports and running unit testsmypy+djangorestframework-stubs- for better typing experiencepsycopg2- needed to use Postgres (in Docker container)gunicorn- production wsgi http serverwhitenoise- building static files
Suggested packages:
drf-yasg- open api documentation (swagger and redoc)django-rest-auth,django-allauth,djoser- making auth easierdjango-filter- enables filtering querysets with url parameters and morepip-chill- works like pip freeze but does not output dependenciespipenv- replacement for venv, works similarly to package.json
- React
react - Typescript
typescript react-router-dom- frontend routingsass- enables scss/sass supportaxios- for making requests
Suggested packages:
- UI libraries such as
Chakra-UI,Material-UI,Reactstrap,TailwindCSSetc. (personally I recommend Chakra-UI) formik+yup- handling form state and validation@reduxjs/toolkit+ required packages (react-redux, redux etc.) - library which makes Redux much easier to understand and usecypress- for e2e testing
Create a virtual environment from cmd (or do it in Pycharm manually)
cd backend
py -3 -m venv venv
venv/Scripts/Activate
python -m pip install --upgrade pip
pip install -r requirements.txtRun django application from cmd (or add new Django configuration if using Pycharm)
python manage.py runserverPreparing (if there are any changes to db schema) and running migrations
python manage.py makemigrations
python manage.py migrateCreate superuser
python manage.py createsuper userInstall node dependencies.
cd frontend
npm iRun development server in second terminal
npm startcd backendRun tests using Coverage instead of python manage.py test
coverage run manage.py testGet report from coverage:
coverage report -mIMPORTANT: You need to change CRLF to LF in entrypoint-dev.sh, entrypoint-prod.sh and entrypoint-build.sh, otherwise build will fail because Linux uses different line endings than Windows. You can do this e.g using Pycharm, choosing LF in Line Separator at the bottom bar. Other files are not affected by this issue.
Create .env file in projects root directory with variables (use your own values):
DB_NAME=postgres
DB_USER=postgres
DB_PASSWORD=postgres
Make sure Docker Engine is running.
While in root directory, build docker images and run them with docker-compose. This might take up to few minutes. Rebuilding image is crucial after installing new packages via pip or npm.
docker-compose up --buildApplication should be up and running: backend 127.0.0.1:8000, frontend 127.0.0.1:3000.
If images had been installed and no changes have been made, just run to start containers:
docker-compose upBringing down containers with optional -v flag removes all attached volumes and invalidates caches.
docker-compose downTo run commands in active container:
docker exec -it <CONTAINER_ID/CONTAINER_NAME> <command>e.g
docker exec -it backend python manage.py createsuperuser
docker exec -it backend coverage run manage.py test
docker exec -it frontend /bin/shIn build configuration changes made locally are not reflected in running application. It is a simulation of the production environment.
The only difference when running containers is that you have to use docker-compose-build.yml file.
To achieve that use -f flag with filename: -f docker-compose-build.yml.
For example:
docker-compose -f docker-compose-build.yml up --build
docker-compose -f docker-compose-build.yml downApplication should be up and running at 127.0.0.1:8000.
Now there is nothing at 127.0.0.1:3000 because static files have already been built.
By default postgres service is shared between dev and build setups. If you want to have a separate db, edit docker-compose-build.yml and set up new volume by yourself.
- Create Heroku Account
- Download/Install/Setup Heroku CLI
- After install, log into Heroku CLI:heroku login - Run:
heroku create <your app name>to create the Heroku application - Set your environment variables for your production environment by running:
heroku config:set PRODUCTION_HOST=<your app name>.herokuapp.com SECRET_KEY=<your secret key> DJANGO_SETTINGS_MODULE=core.settings.prod - Run:
heroku stack:set containerso Heroku knows this is a containerized application - Run:
heroku addons:create heroku-postgresql:hobby-devwhich creates the postgres add-on for Heroku - Deploy your app by running:
git push heroku master,
or by pushing to your github repository,
or manually in Heroku dashboard - Go to
<your app name>.herokuapp.comto see the published website.
If you are having issues with heroku repository, try heroku git:remote -a <your app name>.
heroku run bash --app <your_app_name>e.g
heroku run bash --app django-react-heroku-test
~ $ python backend/manage.py createsuperuser This repository uses Github Actions to run test pipeline.
tests.yml - runs backend and frontend tests separately
I was not able to configure automatic deploys with Github Actions. This was the error.
If you want to enable Automatic Deploys, use Heroku dashboard and enable waiting for CI there.