Vamos aprender a criar um settings para cada ambiente.
Copie o arquivo settings.py e cria uma pasta settings e dentro da pasta crie o arquivo base.py. Lembre de apagar o arquivo settings.py.
Arquivo exemplo project/settings/base.py, não esta completo:
import os
from django.utils.translation import gettext_lazy as _
from django.core.management.utils import get_random_secret_key
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LANGUAGE_CODE = "pt-BR"
LANGUAGES = [
('pt-BR', _('Portuguese')),
('en', _('English')),
('es', _('Spanish')),
]
TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True
Agora crie o arquivo de project/settings/production.py:
import os
from .base import *
DEBUG = False
DATABASES = {
"default": {
"ENGINE": os.getenv("POSTGRES_ENGINE"),
"NAME": os.getenv("POSTGRES_NAME"),
"USER": os.getenv("POSTGRES_USER"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD"),
"HOST": os.getenv("POSTGRES_HOST"),
"PORT": os.getenv("POSTGRES_PORT"),
}
}
MEDIA_URL = "http://media.{}/".format(os.getenv("DOMAIN"))
STATIC_URL = "http://static.{}/".format(os.getenv("DOMAIN"))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
MEDIA_ROOT = os.path.join(BASE_DIR, '..', "media")
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "project/static"),
]
# tinymce
TINYMCE_JS_ROOT = os.path.join(STATIC_ROOT, "path/to/tiny_mce")
Agora crie o arquivo de project/settings/local.py:
import os
from .base import *
DEBUG = True
DATABASES = {
'default': {
"ENGINE": os.getenv("POSTGRES_ENGINE"),
"NAME": os.getenv("POSTGRES_NAME"),
"USER": os.getenv("POSTGRES_USER"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD"),
"HOST": os.getenv("POSTGRES_HOST"),
"PORT": os.getenv("POSTGRES_PORT"),
},
}
MEDIA_URL = "/media/"
STATIC_URL = "/static/"
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
MEDIA_ROOT = os.path.join(BASE_DIR, '..', "media")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Exemplo do docker-compose.yml:
version: "3.9"
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
env_file:
- environments/local/postgresql.env
ports:
- "5432:5432"
expose:
- 5432
web:
build: .
command: python manage.py runserver --settings=project.settings.local 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
env_file:
- environments/local/postgresql.env
- environments/local/django.env
Agora o docker-compose ira forçar o projeto rodar com o settings.local
Lembre de alterar os arquivos manage.py e wsgi.py.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings.local')
Agora pode criar vários settings para diversos ambiente e configurar no docker-compose de cada ambiente.