- 在settings.py旁边创建settings文件夹
- 重命名settings.py为base.py,并移动到新建的settings文件夹中
-
在settings/ 文件夹创建其它的配置文件
以下为Django 2 by example原文
In real-world projects you will have to deal with multiple
environments. You will have at least a local and a production
environment, but you could have other environments as well,
such as testing or pre-production environments. Some project
settings will be common to all environments, but others will
have to be overridden per environment. Let's set up project
settings for multiple environments while keeping everything
neatly organized.
Create a settings/ directory next to the settings.py file of the educa
project. Rename the settings.py file to base.py and move it into
the new settings/ directory. Create the following additional files
inside the setting/ folder so that the new directory looks as
follows:These files are as follows:
base.py: The base settings file that contains common
settings (previously settings.py)
local.py: Custom settings for your local environment
pro.py: Custom settings for the production environment
Edit the settings/base.py file and replace the following line:
BASE_DIR =
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
With the following one:
BASE_DIR =
os.path.dirname(os.path.dirname(os.path.abspath(os.path.join(__f
ile__, os.pardir))))
We have moved our settings files to a directory one level
lower, so we need BASE_DIR to point to the parent directory to be
correct. We achieve this by pointing to the parent directory
with os.pardir.
Edit the settings/local.py file and add the following lines of code:
from .base import *
DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
This is the settings file for our local environment. We import
all settings defined in the base.py file and we only define specific
settings for this environment. We have copied the DEBUG and
DATABASES settings from the base.py file, since these will be set per
environment. You can remove the DATABASES and DEBUG settings
from the base.py settings file.
Edit the settings/pro.py file and make it look as follows:
from .base import *
DEBUG = False
ADMINS = (
('Antonio M', 'email@mydomain.com'),
)
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
}
}
These are the settings for the production environment. Let's
take a closer look at each of them:
DEBUG: Setting DEBUG to False should be mandatory for any
production environment. Failing to do so will result in
traceback information and sensitive configuration data
exposed to everyone.
ADMINS: When DEBUG is False and a view raises an exception,
all information will be sent by email to the people
listed in the ADMINS setting. Make sure to replace the
name/email tuple with your own information.
ALLOWED_HOSTS: Django will only allow the hosts included
in this list to serve the application. This is a security
measure. We include the asterisk symbol * to refer to
all hostnames. We will limit the hostnames that can be
used for serving the application later.
DATABASES: We just keep this setting empty. We are going
to cover database setup for production hereafter.
When handling multiple environments, create a base settings file and a
settings file for each environment. Environment settings files should inherit
the common settings and override environment-specific settings.
We have placed the project settings in a different location than
the default settings.py file. You will not be able to execute any
commands with the manage.py tool unless you specify the
settings module to use. You will need to add a --settings flag
when you run management commands from the shell or set a
DJANGO_SETTINGS_MODULE environment variable.
Open the shell and run the following command:
export DJANGO_SETTINGS_MODULE=educa.settings.pro
This will set the DJANGO_SETTINGS_MODULE environment variable for
the current shell session. If you want to avoid executing this
command for each new shell, add this command to your
shell's configuration in the .bashrc or .bash_profile files. If you
don't set this variable you will have to run management
commands, including the --settings flag, as follows:
python manage.py migrate --settings=educa.settings.pro
You have successfully organized settings for handling multiple
environments.