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

Django dynamic configuration plug-in dynaconf

編輯:Python

1、 Installation and initialization :

# Installing a plug-in
pip install dynaconf
# Initialize configuration file : establish config Folder ,cd config
dynaconf init -f yaml

   

.secret.yaml Configure private information , Important passwords can be put here ,git Ignore without passing it to the library

2、 Enable different configurations for different environments ( That is, the configuration file hierarchy )

 (1) stay config.py Enable the layering function in environment=True,

import os
from dynaconf import Dynaconf
sys_env = os.environ.get('ENV_FOR_DYNACONF')
if sys_env:
env = sys_env
else:
env = 'DEFAULT'
config = Dynaconf(envvar_prefix="BOOKSTORE_WEB_CONF",
settings_files=['configs/settings.yaml'],
environments=True,
env=env) # env Specify the priority configuration layer , If this layer is not found during the call, it will go to the first layer to find 

 (2)settings.yaml Specific layers are listed in , And the name :DEFAULT Layer is the default layer , I can't find object { VAR1 It's a whole , Find... In units of the entire object } Will come here to find , Other layers can be customized : Each time you search the configuration, first specify this layer to search , If you can't find it, go to the default layer

DEFAULT:
NAME: 'DEFAULT'
VAR1:
KEY: 1
NAME: ' test '
VAR2:
KEY: 3
DEVELOPMENT:
NAME: 'DEVELOPMENT'
VAR1:
KEY: 2
PRODUCTION:
NAME: 'PRODUCTION'

Be careful :config.py After layering is turned on in ,settings.yaml There must be a hierarchy in

3、 Server configuration environment variables :~/.bashrc

#export ENV_FOR_DYNACONF=DEFAULT
export ENV_FOR_DYNACONF=DEVELOPMENT

Add configuration and update system variables ,source  ~/.bashrc


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