pythondjangodjango-settingspython-decouple

Django separate settings and .env files (python-decouple): the specified path was not found for static files


I have developed a Django Project and try to manage different configuration (dev and prod) and .env using python-decouple.

Below my project architecture and different config files.

Maybe I misunderstand but in my comprehension :

  1. I have differents settings.py files (dev and prod) that inherit from my "base" settings.py
  2. Some variables must be kept secret: these variable are store in .env files that will not be share (.gitignore)
  3. In production, these secret variables are read from .env in settings files

I run

python manage.py migrate --settings=core.settings.dev

python manage.py runserver --settings=core.settings.dev

and got an error

FileNotFoundError: [WinError 3] The specified path was not found: 'D:\Users\xx\DevSpace\PROJECT_FOLDER\core\static'

That right because static folder is at the same level as core app. But how to configure this path?

- PROJECT_FOLDER
     |_ core
        |_ wsqi.py
        |_ settings
             |_ __init__.py
             |_ .env 
             |_ base.py 
             |_ dev.py
             |_ prod.py
     |_ manage.py
     |_ static
          |_ css
          |_ images
     |_ db.sqlite3

.env

SECRET_KEY=rqps9azjw7i0@_(qxirwr!@0w3f)$prsky9l7bt8t-(y)_tiuj

base.py

from decouple import config

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR,'static'),
    os.path.join(BASE_DIR,'randomization/static'),
    os.path.join(BASE_DIR,'unblind/static'),
    os.path.join(BASE_DIR,'pharmacy/static'),
)

dev.py

from .base import *

SECRET_KEY = 'f!hap7sff#f@8$1iix@(%d4f=88swwetxkhbq=%^x)ga2eowbs'

DEBUG = True

prod.py

from .base import *

SECRET_KEY = config("SECRET_KEY", default="unsafe-secret-key")

DEBUG = False

Solution

  • Add a STATIC_ROOT config:

    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')