0
0
Djangoframework~10 mins

Environment variables for secrets 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 the module used to access environment variables in Django settings.

Django
import [1]
Drag options to blanks, or click blank then click option'
Adjango
Bsys
Cos
Denv
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sys' instead of 'os' to access environment variables.
Trying to import a non-existent module like 'env'.
2fill in blank
medium

Complete the code to get the secret key from environment variables in Django settings.

Django
SECRET_KEY = os.environ.get([1])
Drag options to blanks, or click blank then click option'
A'SECRET_KEY'
B'KEY_SECRET'
C'SECRET'
D'DJANGO_SECRET'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect environment variable names like 'DJANGO_SECRET'.
Forgetting to put the variable name in quotes.
3fill in blank
hard

Fix the error in the code to safely get an environment variable with a default fallback.

Django
DEBUG = os.environ.get([1], 'False') == 'True'
Drag options to blanks, or click blank then click option'
A'DEBUG'
BDEBUG_MODE
Cdebug
D'DEBUG_MODE'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without quotes causing syntax errors.
Using incorrect environment variable names.
4fill in blank
hard

Fill both blanks to load environment variables from a .env file using python-dotenv in Django.

Django
from dotenv import [1]
[2]()
Drag options to blanks, or click blank then click option'
Aload_dotenv
Bfind_dotenv
Cdotenv
Dloadenv
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong function like 'find_dotenv' and calling it incorrectly.
Not calling the function after importing.
5fill in blank
hard

Fill all three blanks to create a dictionary of database settings using environment variables in Django.

Django
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get([1]),
        'USER': os.environ.get([2]),
        'PASSWORD': os.environ.get([3]),
    }
}
Drag options to blanks, or click blank then click option'
A'DB_NAME'
B'DB_USER'
C'DB_PASSWORD'
D'DATABASE'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect or inconsistent environment variable names.
Forgetting to put variable names in quotes.