Static files - Settings

Today we will cover what's required to get static files to actually work in development.

The default Django setup provides just the initial setting of STATIC_URL = 'static/'. This is a good start and it would appear that this is only provided since these other settings make assumptions about the project structure.

There are a minimum of two other changes to your settings for static files to work, first add STATIC_ROOT. This should be a directory relative to your BASE_DIR, for example:

STATIC_ROOT = BASE_DIR / 'static_root'

The static_root directory needs to exist and added to your .gitignore file. This is where all static files will be copied to when collectstatic is executed.

Finally we need either ensure our Django app is listed in INSTALLED_APPS for collectstatic to pick up files within an app or we need to use STATICFILES_DIRS which is a list of directories outside of the Django app structure where static files reside.

The final complete settigs is as follows:

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'static_root'
    BASE_DIR / 'my_project' / 'static'

which has the following file structure:

├── db.sqlite3
├── static_root
└── my_project
    ├── static
    │   └── style.css
    ├── templates
    │   └── index.html

Tomorrow we will expand on this for media files.