简体   繁体   English

使用web接口修改config.ini

[英]Modify config.ini using web interface

I'm trying to change settings on my config.ini我正在尝试更改我的 config.ini 上的设置

[config]
username = foo
password = bar

How do I configure my configparser config.ini using a web interface?如何使用 web 接口配置我的 configparser config.ini?

Your question is not very clear but I'll try to answer it anyway.你的问题不是很清楚,但我还是会尽力回答。

Webinterface网页界面

It's out of scope of a SO answer to show completely how to create a webinterface but as a starting point I'd recommend to walk through this Django tutorial .它超出了 SO 答案的 scope 以完全展示如何创建 Web 界面,但作为起点,我建议您阅读此 Django 教程

Read/Write a config file读/写配置文件

Write your config.ini using configparser:使用 configparser 编写 config.ini:

$ python3
Python 3.7.5 (default, Nov  1 2019, 02:16:32)
>>> import configparser
>>> config = configparser.ConfigParser()
>>> 
>>> config['config'] = {'username': 'foo', 'password': 'bar'}
>>> with open('config.ini', 'w') as configfile:
...     config.write(configfile)
>>> 

Read your config.ini using configparser:使用 configparser 读取您的 config.ini:

$ python3
Python 3.7.5 (default, Nov  1 2019, 02:16:32)
>>> import configparser
>>> config = configparser.ConfigParser()
... config.read('config.ini')
['example.ini']
>>> 
>>> # list sections
... config.sections()
['config']
>>> 
>>> # check if a section exists
... 'config' in config
True
>>> 
>>> # check if field exists in a section
... 'username' in config['config']
True
>>>
>>> # get value of a field in a section
... config['config']['username']
'foo'
>>>
>>> # List all fields in a section
... for key in config['config']:
...     print(key)
...
username
password
>>> 

For more details about configparser, check out its documentation .有关 configparser 的更多详细信息,请查看其文档

If you need to know how to connect your shiny new Django web app to the configparser, ask another, more specific question why and where you are stuck.如果您需要知道如何将您的 shiny new Django web 应用程序连接到 configparser,请询问另一个更具体的问题,为什么以及您卡在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM