Add prelim support for running outside Docker using SQLite and venv

This commit is contained in:
Matt Cengia 2022-12-09 14:38:16 +11:00
parent 592a6d63f3
commit 55218c6843
Signed by: mattcen
GPG Key ID: 652968EE25069D6F
11 changed files with 97 additions and 23 deletions

View File

@ -1 +1,5 @@
.venv/
__pycache__/
static/
media/
db/

View File

@ -1,21 +1,38 @@
POSTGRES_HOST=db
POSTGRES_PORT=5432
POSTGRES_DB=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
# Use SQLite by default
#DB_ENGINE=django.db.backends.sqlite3
# Use SpatiaLite for spatial projects
#DB_ENGINE=django.contrib.gis.db.backends.spatialite
# Use regular Postgres in production
#DB_ENGINE=django.db.backends.postgresql_psycopg2
# Use PostGIS in production for spatial projects
#DB_ENGINE=django.contrib.gis.db.backends.postgis
# If using SQLite or SpatiaLite, you can set the path to the database here.
# If unset, this defaults to '../db/db.sqlite3'.
#DB_NAME=../db/db.sqlite3
# If using Postgres or PostGIS, set these
#POSTGRES_HOST=db
#POSTGRES_PORT=5432
#POSTGRES_DB=postgres
#POSTGRES_USER=postgres
#POSTGRES_PASSWORD=postgres
# SECRET_KEY can be generated with:
# python -c 'from django.core.management import utils; print(utils.get_random_secret_key())'
SECRET_KEY='django-insecure-#8=34bnllv4$f0ln^#km#)5q7ov!e7p)@!%^1_cc_ediw2q=)0'
# Uncomment the below DEBUG line for dev environment
#DEBUG=true
# Set these to something sensible
SUPERUSER_USERNAME=root
SUPERUSER_PASSWORD=root
SUPERUSER_EMAIL=root@localhost
APP_PORT=8000
#TIME_ZONE=UTC
CSRF_TRUSTED_ORIGINS=http://localhost:$APP_PORT
ALLOWED_HOSTS=*
#TIME_ZONE=UTC
# By default, emails get sent to the local console
#EMAIL_BACKEND=django.core.mail.backends.console.EmailBackend
# To set up an SMTP server, set the following (only EMAIL_BACKEND is required)

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
.env
.venv/
__pycache__/
static/
media/
db/

View File

@ -12,13 +12,49 @@ cd repo
cp .env.example .env
```
2. Start up the Docker environment:
2. Edit the `.env` file and review its settings. At a minimum, you'll probably want to uncomment `DEBUG=true` for testing.
```
docker-compose up -f docker-compose.dev.yml -d
```
3. Start up Django
3. Log into admin console at http://localhost:8000/admin as `root`/`root`.
* To do so using Docker:
```sh
docker-compose up -f docker-compose.dev.yml -d
```
* To do so using a Python `venv`:
NOTE: These instructions are only tested on macOS and Linux
Perform the initial set-up (do this only once):
```sh
# Create a virtual environment
python3 -m venv .venv
# Activate virtual environment
. .venv/bin/activate
# Install Python dependencies
pip install -r django/requirements.txt
# Change to the django dir
cd django/
# Run the Docker entrypoint script to set up an initial superuser etc
./docker-entrypoint.sh
cd ..
deactivate
```
Start up Django (do this every time):
```
# Activate virtual environment
. .venv/bin/activate
# Change to the django dir
cd django/
# Start Django
./manage.py runserver
```
4. Log into admin console at http://localhost:8000/admin as `root`/`root`.
## Notes
@ -28,4 +64,6 @@ By default there is no `docker-compose.yml`, so `docker-compose up` won't work w
`docker-compose.prod.yml` is designed for production.
For convenience, either of these files could be copied to `docker-compose.yml` so you don't need to specify the file with `docker-compose -f file.yml`.
`docker-compose.traefik.yml` is designed for production when using Traefik as a HTTP proxy
For convenience, any of these files could be copied or symlinked to `docker-compose.yml` so you don't need to specify the file with `docker-compose -f file.yml`.

0
db/.gitkeep Normal file
View File

View File

@ -8,6 +8,7 @@ RUN apt-get update && \
binutils \
libproj-dev \
gdal-bin \
libsqlite3-mod-spatialite \
# Database client for using './manage.py dbshell' etc
#sqlite3 \
#spatialite-bin \

View File

@ -8,6 +8,7 @@ RUN apt-get update && \
binutils \
libproj-dev \
gdal-bin \
libsqlite3-mod-spatialite \
# Database client for using './manage.py dbshell' etc
sqlite3 \
spatialite-bin \

View File

@ -18,12 +18,12 @@ env = environ.Env(
DEBUG=(bool, False),
TIME_ZONE=(str, 'UTC')
)
# reading .env file
# environ.Env.read_env()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# reading .env file
environ.Env.read_env(BASE_DIR.parent / '.env')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
@ -94,13 +94,17 @@ WSGI_APPLICATION = 'myproject.wsgi.application'
DATABASES = {
'default': {
# Use regular postgres by default
'ENGINE': 'django.db.backends.postgresql_psycopg2',
# Use postgis for spatial projects
#'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': env("POSTGRES_DB"),
'USER': env("POSTGRES_USER"),
'PASSWORD': env("POSTGRES_PASSWORD"),
# Use sqlite by default
'ENGINE': env("DB_ENGINE", default='django.db.backends.sqlite3'),
# Use spatialite for spatial projects
#'ENGINE': env("DB_ENGINE", default='django.contrib.gis.db.backends.spatialite'),
# Use regular Postgres in production
#'ENGINE': env("DB_ENGINE", default='django.db.backends.postgresql_psycopg2'),
# Use PostGIS in production for spatial projects
#'ENGINE': env("DB_ENGINE", default='django.contrib.gis.db.backends.postgis'),
'NAME': env("POSTGRES_DB", default=env("DB_NAME", default=BASE_DIR.parent / 'db/db.sqlite3')),
'USER': env("POSTGRES_USER", default="nobody"),
'PASSWORD': env("POSTGRES_PASSWORD", default="insecure"),
'HOST': env("POSTGRES_HOST", default="localhost"),
'PORT': env("POSTGRES_PORT", default="5432"),
}
@ -144,9 +148,9 @@ USE_TZ = True
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
STATIC_ROOT = BASE_DIR.parent / 'static'
MEDIA_URL = '/media/'
MEDIA_ROOT = '/media/'
MEDIA_ROOT = BASE_DIR.parent / 'media'
# Default primary key field type
# https://docs.djangoproject.com/en/dev/ref/settings/#default-auto-field

View File

@ -29,6 +29,8 @@ services:
- ./django:/code
- static_files:/static
- media_files:/media
# Uncomment this to use SQLite
#- ./db:/db
restart: "unless-stopped"
# To use an external database server, comment out this dependency
depends_on:

View File

@ -28,6 +28,8 @@ services:
volumes:
- static_files:/static
- media_files:/media
# Uncomment this to use SQLite
#- db_data:/db
restart: "unless-stopped"
# To use an external database server, comment out this dependency
depends_on:

View File

@ -28,6 +28,8 @@ services:
volumes:
- static_files:/static
- media_files:/media
# Uncomment this to use SQLite
#- db_data:/db
restart: "unless-stopped"
# To use an external database server, comment out this dependency
depends_on: