This is a Django repo for bootstrapping projects running the WSGI server.
It may act as a template to get a dockerised Django backend setup quickly.
- Docker
- Gunicorn (WSGI)
- Django
- Django Rest Framework
- Pytest
- Loguru
- Install Docker Desktop
- Install pipenv globally:
brew install pipx
pipx install pipenv
pipx ensurepathCopy required env vars:
cp .env.example .envSet your variables accordingly:
DJANGO_PROJECT_NAMESet your Django project name by replace this value (defaultproject).
IMPORTANT: Do not change this value after first spinning up Docker; a new project will be created with django-admin startproject ${DJANGO_PROJECT_NAME} if the named project directory and its settings file do not exist. Alternatively, hardcode your preferred name and remove all instances of the var (recommended for more established projects).
-
DEBUGSetDEBUG=1for development (default1). This is both for Django dev and to direct gunicorn to reload file changes. -
WORKERSSets the number of worker instances for gunicorn to use when not in dev (default1).
docker compose build
docker compose upUse ctrl+c in the terminal running the containers to shutdown and exit.
This setup uses pytest rather that Django's built in test framework. I find it has a cleaner syntax and parametrisation is a big win.
A separate test database volume and docker compose file are used to run tests. This makes clear separation of test and development databases, and mirrors how tests ought to be run in production. To run tests, use the docker-compose.test.yml:
docker compose -f docker-compose.test.yml up --build --abort-on-container-exitOr use the helper script (see next).
There are executable scripts found in the root level bin.
collectstatic: refreshes Django's static filesmakemigrations: makes Django migrations, excepts args (see script for usage)migrate: runs migrationstest: runs testsdown: removes main and test containers
Run these from root like a command, e.g.:
bin/testFeel free to add to and/or pair back this setup to your heart's content.
Made by @chilico