0
0
Djangoframework~30 mins

WhiteNoise for static files in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Using WhiteNoise to Serve Static Files in Django
📖 Scenario: You are building a simple Django website that needs to serve static files like CSS and images efficiently in production without extra server setup.
🎯 Goal: Learn how to configure WhiteNoise in a Django project to serve static files directly from the Django app.
📋 What You'll Learn
Create a list of installed apps including 'whitenoise.runserver_nostatic'
Add WhiteNoise middleware to the Django middleware list
Set the STATIC_ROOT variable to a specific folder path
Configure STATICFILES_STORAGE to use WhiteNoise's storage backend
💡 Why This Matters
🌍 Real World
Websites often need to serve CSS, JavaScript, and images efficiently. WhiteNoise helps Django projects serve these files directly without extra server setup.
💼 Career
Knowing how to configure static file serving with WhiteNoise is useful for Django developers deploying apps to production environments.
Progress0 / 4 steps
1
Setup INSTALLED_APPS with WhiteNoise
In your Django settings.py, create a list called INSTALLED_APPS that includes 'whitenoise.runserver_nostatic' and 'django.contrib.staticfiles' exactly in that order.
Django
Need a hint?

Remember to list 'whitenoise.runserver_nostatic' first, then 'django.contrib.staticfiles'.

2
Add WhiteNoise Middleware
Add a list called MIDDLEWARE in settings.py that includes 'django.middleware.security.SecurityMiddleware' as the first item and 'whitenoise.middleware.WhiteNoiseMiddleware' as the second item.
Django
Need a hint?

Middleware order matters: SecurityMiddleware first, then WhiteNoiseMiddleware.

3
Set STATIC_ROOT for Collecting Static Files
Add a variable STATIC_ROOT in settings.py and set it to BASE_DIR / 'staticfiles'. Assume BASE_DIR is already defined as the project base path.
Django
Need a hint?

Use the / operator to join BASE_DIR with the folder name 'staticfiles'.

4
Configure STATICFILES_STORAGE for WhiteNoise
Add a variable STATICFILES_STORAGE in settings.py and set it to the string 'whitenoise.storage.CompressedManifestStaticFilesStorage'.
Django
Need a hint?

This setting tells Django to use WhiteNoise's static files storage with compression and caching.