0
0
Djangoframework~10 mins

WhiteNoise for static files in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import WhiteNoise middleware in Django settings.

Django
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '[1]',
    'django.contrib.sessions.middleware.SessionMiddleware',
]
Drag options to blanks, or click blank then click option'
A'django.middleware.csrf.CsrfViewMiddleware'
B'django.middleware.common.CommonMiddleware'
C'whitenoise.middleware.WhiteNoiseMiddleware'
D'django.middleware.clickjacking.XFrameOptionsMiddleware'
Attempts:
3 left
💡 Hint
Common Mistakes
Using other middleware names instead of WhiteNoiseMiddleware.
Not adding WhiteNoise middleware at all.
2fill in blank
medium

Complete the code to set the static files storage to WhiteNoise's compressed manifest storage.

Django
STATICFILES_STORAGE = '[1]'
Drag options to blanks, or click blank then click option'
A'whitenoise.storage.CompressedManifestStaticFilesStorage'
B'django.contrib.staticfiles.storage.StaticFilesStorage'
C'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
D'whitenoise.storage.StaticFilesStorage'
Attempts:
3 left
💡 Hint
Common Mistakes
Using default Django staticfiles storage which lacks compression.
Using wrong WhiteNoise storage class names.
3fill in blank
hard

Fix the error in the code to correctly wrap the Django WSGI application with WhiteNoise.

Django
from django.core.wsgi import get_wsgi_application
from whitenoise import WhiteNoise

application = get_wsgi_application()
application = [1](application, root='/path/to/static')
Drag options to blanks, or click blank then click option'
AWhiteNoiseApplication
BWhiteNoiseMiddleware
CWhiteNoiseWrapper
DWhiteNoise
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like WhiteNoiseMiddleware.
Not wrapping the application at all.
4fill in blank
hard

Fill both blanks to configure WhiteNoise to add custom headers and enable max-age caching.

Django
application = WhiteNoise(application, root='/static')
application.add_headers('/static', [1])
application.max_age = [2]
Drag options to blanks, or click blank then click option'
A{'Cache-Control': 'max-age=31536000, public'}
B{'Cache-Control': 'no-cache'}
C31536000
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using string instead of dictionary for headers.
Setting max_age to zero disables caching.
5fill in blank
hard

Fill all three blanks to create a WhiteNoise middleware setup that compresses files, serves static files from 'staticfiles', and sets max-age to one day.

Django
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    [1],
]

STATICFILES_STORAGE = [2]

application = get_wsgi_application()
application = WhiteNoise(application, root='[3]')
application.max_age = 86400
Drag options to blanks, or click blank then click option'
A'whitenoise.middleware.WhiteNoiseMiddleware'
B'whitenoise.storage.CompressedManifestStaticFilesStorage'
Cstaticfiles
D'django.middleware.common.CommonMiddleware'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong middleware or storage strings.
Incorrect static root path.