0
0
Djangoframework~15 mins

Environment-based settings in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Environment-based settings in Django
📖 Scenario: You are building a Django web application that needs to use different settings depending on whether it is running in development or production. This helps keep sensitive information safe and makes your app flexible.
🎯 Goal: Create a Django settings setup that loads a secret key and debug mode from environment variables. This way, the app behaves differently in development and production without changing the code.
📋 What You'll Learn
Create a SECRET_KEY variable in settings.py that reads from environment variable DJANGO_SECRET_KEY
Create a DEBUG variable in settings.py that reads from environment variable DJANGO_DEBUG and converts it to a boolean
Set a default value for DEBUG as False if the environment variable is not set
Use Python's os module to access environment variables
💡 Why This Matters
🌍 Real World
Many Django projects use environment variables to keep sensitive data like secret keys and debug flags out of the codebase. This makes deployment safer and easier.
💼 Career
Understanding environment-based settings is essential for Django developers to build secure and flexible applications that work well in different environments.
Progress0 / 4 steps
1
Import the os module
In your settings.py file, import the os module to access environment variables.
Django
Need a hint?

Use import os at the top of your settings.py file.

2
Create SECRET_KEY from environment variable
Create a variable called SECRET_KEY in settings.py that gets its value from the environment variable DJANGO_SECRET_KEY using os.environ.get().
Django
Need a hint?

Use os.environ.get('DJANGO_SECRET_KEY') to read the secret key.

3
Create DEBUG variable from environment variable
Create a variable called DEBUG in settings.py that reads from the environment variable DJANGO_DEBUG. Convert the string value to a boolean by checking if it equals 'True'. If the environment variable is not set, default DEBUG to False.
Django
Need a hint?

Use os.environ.get('DJANGO_DEBUG', 'False') == 'True' to convert the string to boolean.

4
Complete environment-based settings setup
Ensure your settings.py file has the import os line, the SECRET_KEY variable reading from DJANGO_SECRET_KEY, and the DEBUG variable reading from DJANGO_DEBUG with a default of False and converted to boolean.
Django
Need a hint?

Check that all three lines are present exactly as shown.