上QQ阅读APP看书,第一时间看更新
How to do it...
Follow these steps to configure project settings:
- In the myproject directory, create a config Python module with the following files:
- __init__.py
- base.py for shared settings
- dev.py for development settings
- test.py for testing settings
- staging.py for staging settings
- prod.py for production settings
- Put all of your shared settings in config/base.py.
- If the settings of an environment are the same as the shared settings, then just import everything from base.py there, as follows:
# myproject/config/prod.py
from .base import *
- Apply the settings that you want to attach or overwrite for your specific environment in the other files, for example, the development environment settings should go to dev.py, as shown in the following:
# myproject/config/dev.py
from .base import *
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
- At the beginning of myproject/settings.py, import the configurations from one of the environment settings and then additionally attach specific or sensitive configurations, such as DATABASES or API keys that shouldn't be under version control, as follows:
# myproject/settings.py
from .config.dev import *
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "myproject",
"USER": "root",
"PASSWORD": "root",
}
}
- Create a settings.py.example file that should contain all the sensitive settings that are necessary for a project to run, however, with empty values set.