首页 > 编程语言 > 详细

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

时间:2017-01-19 13:12:58      阅读:691      评论:0      收藏:0      [点我收藏+]

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!