程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Configure multiple settings in Django Py file

編輯:Python

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 .

modify settings File path

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 .

Create a production environment configuration

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 .

Configure privacy items such as passwords

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved