Performance: Environment variables for secrets
This affects page load speed indirectly by reducing server startup delays and preventing blocking caused by secret management errors.
import os SECRET_KEY = os.getenv('DJANGO_SECRET_KEY') DATABASE_PASSWORD = os.getenv('DATABASE_PASSWORD')
SECRET_KEY = 'hardcoded-secret-key' DATABASE_PASSWORD = 'hardcoded-password'
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Hardcoded secrets in code | 0 | 0 | 0 | [X] Bad |
| Environment variables for secrets | 0 | 0 | 0 | [OK] Good |
DJANGO_SECRET_KEY in Django's settings.py?os.getenv('VAR_NAME') safely reads environment variables.settings.py:
import os
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'defaultkey')
print(SECRET_KEY)
What will be printed if the environment variable DJANGO_SECRET_KEY is not set?os.getenv returns the default value provided as the second argument.DJANGO_SECRET_KEY is not set, os.getenv returns 'defaultkey', which is printed.settings.py:
import os SECRET_KEY = os.getenv(DJANGO_SECRET_KEY)But when running the server, you get a NameError. What is the problem?
DJANGO_SECRET_KEY is not defined as a variable in the code.os.getenv('DJANGO_SECRET_KEY').DJANGO_SECRET_KEY differently in each environment and reading it via os.getenv allows safe, flexible secret management without code changes.Pass the test and this pattern moves unknown