The project is in the development environment and the actual production environment ,settings.py
Configuration files are usually different , And according to the previous development experience , After the completion of project development settings.py
It usually stores some user names and passwords , Therefore, these privacy configuration items need to be configured separately .
stay setting.py
Create a parent directory of Python package
Folder , Creating a new folder directly is also ok Of , But we need to create another one __init__.py
The statement of .
And then the original settings.py
File move to settings
In the folder , Change the file name to base.py
, Basic settings ( It can be understood as an item with the same configuration of the development environment and the production environment ).
In this case, if you directly use python manage.py runserver
The command will report an error , because manage.py
It's defined in it settings.py
The default path to the file , Need to be in manage.py
Modify the path of the setting file in , I'm here to meetingroom.settings
It is amended as follows settings.base
def main():
# take meetingroom.settings It is amended as follows settings.base
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'meetingroom.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.base')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
Start the service at this time , Will call by default settings
Under the folder base.py
Configuration of .
stay settings
Create a new folder production.py
file , Used to configure the configuration in the production environment .
# production.py
from .base import *
# Usually use nginx Mapping out , So just set it up settings Just go
ALLOWED_HOSTS = ['127.0.0.1']
DEBUG = False
# For more information, please configure on demand
At this time to run python manage.py runserver --setting settings.production
That is, you can start the service by configuring the production environment .
stay base.py
in , Configure the privacy item to None
...
LOCAL_TEST_USERNAME = None
LOCAL_TEST_PASSWORD = None
...
Then in the local configuration file local.py
Or other configuration files
from .base import *
TEST_USERNAME = 'hsinyan'
TEST_PASSWORD = 'root'
This configuration is sufficient , It can avoid the leakage of sensitive information in the test environment .