0
0
Djangoframework~20 mins

Environment variables for secrets in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variables Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output of this Django settings snippet?
Given the following Django settings code that reads a secret key from environment variables, what will be the value of SECRET_KEY if the environment variable DJANGO_SECRET_KEY is not set?
Django
import os

SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'default_secret')
print(SECRET_KEY)
Adefault_secret
B'' (empty string)
CNone
DRaises KeyError
Attempts:
2 left
💡 Hint
Check what os.getenv returns when the environment variable is missing and a default is provided.
component_behavior
intermediate
1:30remaining
How does Django behave when SECRET_KEY is set from environment variables?
If you set SECRET_KEY = os.environ['DJANGO_SECRET_KEY'] in your Django settings and the environment variable DJANGO_SECRET_KEY is missing, what happens when you run the server?
Django
import os

SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
ADjango uses an empty string as SECRET_KEY
BDjango raises a KeyError and stops running
CDjango uses a default secret key automatically
DDjango logs a warning but continues with a random key
Attempts:
2 left
💡 Hint
What does os.environ['VAR'] do if VAR is not set?
🔧 Debug
advanced
2:00remaining
Why does this Django app fail to load the secret key from environment variables?
Consider this snippet in settings.py:
import os

SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')

if not SECRET_KEY:
    raise Exception('Missing SECRET_KEY environment variable')
The app crashes with the exception even though the environment variable is set. What is the most likely cause?
Django
import os

SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')

if not SECRET_KEY:
    raise Exception('Missing SECRET_KEY environment variable')
AThe environment variable is set but empty string, which is falsy
Bos.getenv cannot read environment variables in Django
CSECRET_KEY is set but the code has a syntax error
DThe environment variable name is case-insensitive and mismatched
Attempts:
2 left
💡 Hint
Check what happens if the environment variable exists but is empty.
📝 Syntax
advanced
1:30remaining
Which option correctly loads a secret key from environment variables with a fallback in Django?
Choose the correct Python code snippet to load SECRET_KEY from the environment variable DJANGO_SECRET_KEY with a fallback value 'fallback_secret' if the variable is not set.
ASECRET_KEY = os.environ['DJANGO_SECRET_KEY'] or 'fallback_secret'
BSECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'fallback_secret')
CSECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'fallback_secret')
DSECRET_KEY = os.getenv('DJANGO_SECRET_KEY') ?? 'fallback_secret'
Attempts:
2 left
💡 Hint
Check Python syntax for getting environment variables with default values.
🧠 Conceptual
expert
2:00remaining
Why is it recommended to use environment variables for Django secrets instead of hardcoding them?
Select the best reason why using environment variables for secrets like SECRET_KEY is preferred over hardcoding them in Django settings.
AIt allows Django to automatically rotate secrets without code changes
BIt makes the app run faster by loading secrets from memory
CIt enables secrets to be stored in the database securely
DIt keeps secrets out of source code, reducing risk if code is shared or published
Attempts:
2 left
💡 Hint
Think about security and code sharing.