python環境下django增加讀取ini配置文件功能

1、安裝ConfigParser

pip install ConfigParser

2、在django目錄配置ini文件的路徑

TEST_CONF_DIR = os.path.join(BASE_DIR, "common/conf/test_conf.ini")

3、寫一個ini文件到公共目錄

common/conf/test_conf.ini

[test_robot_conf]
robot_servergroup_hz = test1
robot_servergroup_bj = test2
robotversionid = test1.0
robotagentid = test2.0
test_url =  http://www.test.com/



4、views層獲取數據

website/views.py

import configparser
test_path = settings.TEST_CONF_DIR
print("test_path",test_path)
cf = configparser.ConfigParser()
cf.read('test_path')
test_urls = cf.get("test_robot_conf", "test_url")  # 讀取配置文件
print("test_urls", test_urls)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章