You want to serve static files efficiently with WhiteNoise and also enable long-term caching. Which combination of settings is best?
hard📝 state output Q15 of 15
Django - Deployment and Production
You want to serve static files efficiently with WhiteNoise and also enable long-term caching. Which combination of settings is best?
AAdd WhiteNoise middleware after SecurityMiddleware and set STATICFILES_STORAGE to 'whitenoise.storage.CompressedManifestStaticFilesStorage'
BAdd WhiteNoise middleware at the start of MIDDLEWARE and set STATICFILES_STORAGE to 'django.contrib.staticfiles.storage.StaticFilesStorage'
CDo not add WhiteNoise middleware and set STATICFILES_STORAGE to 'whitenoise.storage.CompressedStaticFilesStorage'
DAdd WhiteNoise middleware after SessionMiddleware and set STATICFILES_STORAGE to 'django.contrib.staticfiles.storage.StaticFilesStorage'
Step-by-Step Solution
Solution:
Step 1: Middleware placement for WhiteNoise
WhiteNoise middleware must be placed immediately after SecurityMiddleware for proper static file serving.
Step 2: Choosing STATICFILES_STORAGE for caching
'CompressedManifestStaticFilesStorage' compresses files and adds hashes for long-term caching, which is best practice.
Step 3: Evaluate other options
Other options either place middleware incorrectly or use storage classes that don't support long-term caching well.
Final Answer:
Add WhiteNoise middleware after SecurityMiddleware and set STATICFILES_STORAGE to 'whitenoise.storage.CompressedManifestStaticFilesStorage' -> Option A
Quick Check:
Correct middleware order + caching storage = B [OK]
Quick Trick:Middleware after SecurityMiddleware + CompressedManifestStaticFilesStorage for caching [OK]
Common Mistakes:
MISTAKES
Placing middleware too early or late
Using default storage without hashing
Skipping middleware but setting storage
Using storage without compression or hashing
Master "Deployment and Production" in Django
9 interactive learning modes - each teaches the same concept differently