0
0
Djangoframework~20 mins

Environment-based settings in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Settings Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django settings code snippet?
Given the following Django settings snippet that uses environment variables, what will be the value of DEBUG if the environment variable DJANGO_DEBUG is not set?
Django
import os

DEBUG = os.getenv('DJANGO_DEBUG', 'False') == 'True'
AFalse
BNone
CRaises KeyError
DTrue
Attempts:
2 left
💡 Hint
Remember that os.getenv returns the default value if the environment variable is missing.
📝 Syntax
intermediate
2:00remaining
Which option correctly loads a secret key from environment variables in Django settings?
Select the option that correctly assigns the SECRET_KEY from an environment variable named DJANGO_SECRET_KEY and raises an error if it is missing.
ASECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
BSECRET_KEY = os.environ['DJANGO_SECRET_KEY']
CSECRET_KEY = os.getenv('DJANGO_SECRET_KEY', '')
DSECRET_KEY = os.getenv('DJANGO_SECRET_KEY') or raise ValueError('Missing key')
Attempts:
2 left
💡 Hint
Using os.environ with square brackets raises an error if the variable is missing.
state_output
advanced
2:00remaining
What is the value of DATABASES['default']['PORT'] after running this Django settings code?
Consider this snippet in Django settings.py. What will be the port value used by the database configuration?
Django
import os

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv('DB_NAME', 'mydb'),
        'USER': os.getenv('DB_USER', 'user'),
        'PASSWORD': os.getenv('DB_PASS', 'pass'),
        'HOST': os.getenv('DB_HOST', 'localhost'),
        'PORT': int(os.getenv('DB_PORT', '5432'))
    }
}
A5432
BNone
CRaises ValueError
D0
Attempts:
2 left
💡 Hint
Check the default value and the type conversion used.
🔧 Debug
advanced
2:00remaining
Which option causes a runtime error when loading environment-based settings?
Given this Django settings snippet, which option will cause a runtime error when the environment variable ALLOWED_HOSTS is not set?
Django
import os

ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS').split(',')
AALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '').split(',')
BALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS').split(',') if os.environ.get('ALLOWED_HOSTS') else []
CALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',')
DALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS').split(',')
Attempts:
2 left
💡 Hint
What happens if os.getenv returns None and you call split() on it?
🧠 Conceptual
expert
3:00remaining
Which approach best ensures secure environment-based settings in Django for production?
You want to keep sensitive settings like SECRET_KEY and database credentials secure and configurable per environment. Which approach below is the best practice?
AStore secrets in a JSON file committed to the repo and read it in settings.py
BHardcode all secrets in settings.py and use gitignore to exclude it from version control
CUse environment variables and a secrets manager or vault in production, with .env files only for local development
DUse environment variables accessed via os.environ and load them with a .env file in development only
Attempts:
2 left
💡 Hint
Consider security and flexibility for different environments.