WhiteNoise allows Django to serve static files directly, even in production, without needing a separate web server like Nginx. This simplifies deployment.
WhiteNoise middleware must be placed near the top of the MIDDLEWARE list, usually right after SecurityMiddleware, to serve static files correctly.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# other middleware
]
STATIC_ROOT = '/var/www/static/'
# collectstatic has been run
# DEBUG = False
In production, WhiteNoise requires STATICFILES_STORAGE to be set to 'whitenoise.storage.CompressedManifestStaticFilesStorage' to serve compressed and versioned static files correctly.
WhiteNoise works with Django's collectstatic to copy static files into STATIC_ROOT, adding hashes to filenames for cache busting and compressing files for faster delivery.
WhiteNoise allows Django apps to serve static files directly, reducing deployment complexity by avoiding separate static file server setup and maintenance.