[英]Web server ini-file editor using django
I wish to edit ini files over web server, decided to use django, been using it for few days now. 我希望通过Web服务器编辑ini文件,决定使用django,现在已经使用了几天。 I can't figure out how to accomplish this. 我不知道如何做到这一点。 I have ini file structure looking like this: 我的ini文件结构如下所示:
{'GROUP', {PROPERTY : VALUE}}
Example when I read this kind of ini file: 当我阅读这种ini文件的例子:
[LOG]
FilePath = C:/Log
[CMD]
Level = 5
I will get my data structure filled like this: 我将像这样填充数据结构:
{'LOG', {'FilePath' : 'C:/Log',},
{'CMD', {'Level', '5'}}}
Loop looks like this: 循环看起来像这样:
for group in settingsDict:
print group # group
for property in settingsDict[group]:
print property , # property
print settingsDict[group][property] # value
I am using ini file parser . 我正在使用ini文件解析器 。
I am having trouble understanding how to correctly develop in django: views.py
is some kind of controller for django and templates are views and model would be my ini file (probably linked with db using django model), or am I getting something wrong? 我在理解如何正确地在django中开发时遇到了麻烦: views.py
是django的某种控制器,模板是视图和模型,将是我的ini文件(可能是使用django模型与db链接),还是我出了点问题?
I have no problem passing this dictionary to template, making a for loop in it and creating html tags like: <input type="text" name={{ property }} value={{ value }} maxlength="100" />
. 我毫不费力地将此字典传递给模板,在其中进行for循环并创建html标签,例如: <input type="text" name={{ property }} value={{ value }} maxlength="100" />
。 But how do I then post all the edited values back to control to save them in file (or db)? 但是,然后我该如何将所有编辑后的值发回控制以将它们保存在文件(或数据库)中呢? I Would need all 3 values, that is GROUP, PROPERTY and VALUE
. 我将需要所有3个值,即GROUP, PROPERTY and VALUE
。
Then I discovered django also has html widgets, which you create in views.py
and then pass it to template. 然后我发现django也有html小部件,您可以在views.py
创建它,然后将其传递给模板。 But this is where I stop understanding things, since I am creating widget in my controller class, but even if I am. 但这是我停止理解事物的地方,因为我正在控制器类中创建小部件,即使我是。
Shall I create a list of all django widgets and pass it to template? 我应该创建所有django小部件的列表并将其传递给模板吗? Same question occurs, how do I get all the widget values back to controller (views.py)? 发生相同的问题,如何将所有小部件值都返回给控制器(views.py)?
Update (11.6.2012): My code looks like this: views.py 更新(11.6.2012):我的代码如下:views.py
class DynForm(forms.Form):
def setFields(self, kwds):
keys = kwds.keys()
keys.sort()
for k in keys:
self.fields[k] = kwds[k]
def settings(request):
global Settings #my ini dict
kwargs = {}
for group in Settings:
for property in Settings[group]:
kwargs[property] = forms.CharField(label = property, initial = Settings[group][property])
f = DynForm()
f.setFields(kwargs)
return render_to_response('/settings.html',
{
'textWidget' : f,
})
@csrf_exempt
def save(request):
if request.method == 'POST': # If the form has been submitted...
form = DynForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# process form data
# and return response
settings.html settings.html
<form action="/save/" method="post">
{% csrf_token %}
{% for field in textWidget %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Save" /></p>
</form>
The problem is, DynForm(request.POST)
returns null so I can't get field values. 问题是, DynForm(request.POST)
返回null,所以我无法获取字段值。 My request.POST is correct, containing all fields and values. 我的request.POST是正确的,包含所有字段和值。 As much as I know, I am not suppose to parse request.POST data "by hands"? 据我所知,我不是应该解析request.POST数据吗?
OK, finally figured it out, taking me a lot of time (I am lacking a lot of python and django knowledge). 好的,终于弄清楚了,这花了我很多时间(我缺少很多python和django知识)。 I can't paste final solution because of copy right permissions, here is the concept: 由于版权保护,我无法粘贴最终解决方案,这是概念:
Form
class DynamicForm(forms.Form):
def __init__(self,*k,**kw):
forms.Form.__init__(self,*k,**kw)
# loop over data from **kw
# create field
# set field default value
Notes about this code: 关于此代码的注释:
super(SuperForm, self).__init__(*args, **kwargs)
, you must use forms.Form.__init__(self,*k,**kw)
so you can append fields to form using self.fields attribute. 如果form不使用super(SuperForm, self).__init__(*args, **kwargs)
,则必须使用forms.Form.__init__(self,*k,**kw)
以便可以使用self将字段附加到form .fields属性。 self.data[field] = defVal
not initial = defVal
. 如果需要使用默认字段值,请使用self.data[field] = defVal
而不是initial = defVal
。 Form becomes unbound and you won't be able to parse data in your request.POST method. 表单变为未绑定,您将无法解析request.POST方法中的数据。 Unbound form (and with errors) will always return is_valid() False
. 未绑定的表单(有错误)将始终返回is_valid() False
。 With this class, you have no problems parsing request.POST
data. 使用此类,您可以轻松解析request.POST
数据。 Looping over dynamic form fields looks like this: 循环遍历动态表单字段如下所示:
View
for name,field in form.fields.items():
# name - field name
# form.data[name] - field value
Notes: 笔记:
Template code loops over fields in form displaying field label and value separated with :
模板代码在表单中的字段上循环显示字段标签和值,并用分隔:
Template
<form action="/Tris/save/" method="post">
{% csrf_token %}
{% for field in textWidget %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.non_field_errors }}
{{ field.label }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Save" /></p>
</form>
Most of the solution is from here: http://jacobian.org/writing/dynamic-form-generation/ and django documentation. 大多数解决方案都来自这里: http : //jacobian.org/writing/dynamic-form-generation/和django文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.