python环境下django增加读取ini配置文件功能

时间:2017-01-19 13:12:58   收藏:0   阅读:691

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)



本文出自 “苦咖啡's运维之路” 博客,请务必保留此出处http://alsww.blog.51cto.com/2001924/1893066

原文:http://alsww.blog.51cto.com/2001924/1893066

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!