
上QQ阅读APP看书,第一时间看更新
How to do it...
Follow these steps to configure the project settings:
- In the myproject directory, create a settings Python module with the following files:
- __init__.py makes the settings directory a Python module.
- _base.py for shared settings
- dev.py for development settings
- test.py for testing settings
- staging.py for staging settings
- production.py for production settings
- Copy the contents of settings.py, which was automatically created when you started a new Django project, to settings/_base.py. Then, delete settings.py.
- Change the BASE_DIR in the settings/_base.py to point one level up. It should first look as follows:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
After changing it, it should look like the following:
BASE_DIR = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
- If the settings of an environment are the same as the shared settings, then just
import everything from _base.py there, as follows:
# myproject/settings/production.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 code snippet:
# myproject/settings/dev.py
from ._base import *
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
- Modify the manage.py and myproject/wsgi.py files to use one of the environment settings by default by changing the following line:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
- You should change this line to the following:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings.production')