Search code examples
djangowhitenoise

How to get Django StaticFileStorage to 500 when Debug is True


I'm trying to get our Django app to throw 500s when an asset is not available - rather than somewhat silently throwing 404s. We want this so that our end-to-end tests catch an asset missing before it goes to prod and starts failing.

I have WHITENOISE_MANIFEST_STRICT set to True, and I believe that Whitenoise is working - I'm using the --nostatic flag, and see the WHITENOISE_MAX_AGE in the Cache-Control Response Headers (although the assets themselves don't have the hash that we see in production or when Debug is set to false).

However I'm still getting 404s for an asset that doesn't exist. Unfortunately we can't use DEBUG = False because the tests themselves depend on Debug being true, so any advice would be super appreciated!


Solution

  • It turns out this behavior is different because when DEBUG is enabled, Django does not do the hash file resolution that is standard.

    You can override the behavior using the file resolution: Force Django ManifestStaticFilesStorage to always return urls with hashes