Search code examples
djangodjango-staticfiles

Django 4.1 Static Files


I recently migrated from Django 3.8 to 4.1 without much issue, but since then, when I have DEBUG=True in my development environment, changes to Static Files are not reflected when refreshing the page, I instead have to reboot my dev environment. All other changes, including template changes and view changes are working as expected, just not static file changes.

To fix the template changes issue I updated my templates variable from:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR/'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.request',
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

to:

default_loaders = [
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
]

cached_loaders = [("django.template.loaders.cached.Loader", default_loaders)]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR/'templates'],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.request',
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': default_loaders if DEBUG else cached_loaders,
        },
    },
]

Which worked perfectly, however still, changes to my static files are not being served. I have tried disabling my browser cache with no luck.

I am using Docker to run the dev environment with Docker Compose.

Let me know if you need any other information, wasn't 100% sure what to provide as I have no idea what may be causing it.


Solution

  • Turns out when I updated my storages config I accidentally put the incorrect location for the staticfiles key.

    I had done:

    STORAGES = {
        "default": {
            "BACKEND": "django.core.files.storage.FileSystemStorage"
        },
        "private": {
            "BACKEND": "django.core.files.storage.FileSystemStorage"
        },
        "staticfiles": {
            "BACKEND": "django.core.files.storage.FileSystemStorage"
        }
    }
    

    When it should be:

    STORAGES = {
        "default": {
            "BACKEND": "django.core.files.storage.FileSystemStorage"
        },
        "private": {
            "BACKEND": "django.core.files.storage.FileSystemStorage"
        },
        "staticfiles": {
            "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
        }
    }