I’ve seen other posts about this such as here but I still can’t figure out my issue.
File structure of relevant files:
ba_django_project
| docker-compose.prod.yml
|
|__app
| Dockerfile.prod
| manage.py
| .env.prod
|
|__ba_django_project
| __init__.py
| settings.py
| wsgi.py
docker-compose.prod.yml:
version: '3.8'
services:
web:
build:
context: ./app
dockerfile: Dockerfile.prod
image: 370950449536.dkr.ecr.us-east-2.amazonaws.com/ba_django_project:ba_django_project_web
command: gunicorn ba_django_project.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
expose:
- 8000
env_file:
- ./app/.env.prod
depends_on:
- db
db:
image: postgres:13.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./app/.env.prod.db
nginx:
build: ./nginx
image: 370950449536.dkr.ecr.us-east-2.amazonaws.com/ba_django_project:ba_django_project_nginx
volumes:
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
ports:
- 1337:80
depends_on:
- web
volumes:
postgres_data:
static_volume:
media_volume:
settings.py:
from pathlib import Path
from decouple import config
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_DIR = os.path.join(BASE_DIR, "static")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = int(os.environ.get("DEBUG", default=0))
ALLOWED_HOSTS = [
'ec2-3-20-22-254.us-east-2.compute.amazonaws.com', '0.0.0.0', 'localhost', '127.0.0.1'
]
# Application definition
INSTALLED_APPS = [
'crispy_forms',
'users.apps.UsersConfig',
'resume.apps.ResumeConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
# etc. can post full version if needed
.env.prod:
SECRET_KEY='secretkeyfiller'
manage.py:
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ba_django_project.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ba_django_project.settings')
application = get_wsgi_application()
I wasn’t getting this error before I started trying to get the app to run on a server. Only possible solutions it seems are either I messed up something with DJANGO_SETTINGS_MODULE
or there’s some sort or circular dependency I can’t find. I’ve looked through all my files to try to find files importing settings and I took out their references to settings and it still won’t work.
2
Answers
Although I could have sworn I double checked this, when I went to echoed my .env file into my server, the special characters in the secret key caused it to skip that line. This can help with inputting special characters using
sed
.If that wasn't the issue, I also removed the quotes around my secret key.
You must have messed up with your SECRET_KEY while you were trying to secure it using
.env
.You must have an actual secret key. E.g:
SECRET_KEY=QhJgFHhvNJFFHjFfdH....
in the.env
file. If you don’t have it, there are ways to generate it using this guide