Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:22:26+00:00 2026-06-05T17:22:26+00:00

I wish to edit ini files over web server, decided to use django, been

  • 0

I wish to edit ini files over web server, decided to use django, been using it for few days now. I can’t figure out how to accomplish this. I have ini file structure looking like this:

{'GROUP', {PROPERTY : VALUE}}

Example when I read this kind of ini file:

[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.

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?

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" />. 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.

Then I discovered django also has html widgets, which you create in views.py and then pass it to template. 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? Same question occurs, how do I get all the widget values back to controller (views.py)?

Update (11.6.2012):
My code looks like this:
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

<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. My request.POST is correct, containing all fields and values. As much as I know, I am not suppose to parse request.POST data “by hands”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:22:29+00:00Added an answer on June 5, 2026 at 5:22 pm

    OK, finally figured it out, taking me a lot of time (I am lacking a lot of python and django knowledge). 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:

    • If form doesn’t use 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.
    • If you need to use default field value, use self.data[field] = defVal not initial = defVal. Form becomes unbound and you won’t be able to parse data in your request.POST method. Unbound form (and with errors) will always return is_valid() False.

    With this class, you have no problems parsing request.POST data. 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:

    • For the sake of simplisity use @csrf_exempt tag before POST method. See http://jordanmessina.com/2010/05/24/django-1-2-csrf-verification-failed/

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you have parent-child tables and you wish to use a DetailsView to edit
I wish to use a function with an arbitrary number of arguments to edit
I have a MYSQL database I wish to edit. In the database under the
I wish to send an email from my localhost machine (using PHPs mail function)
I have a simple form that I wish to postback to the server in
I have a simple form in codeigniter that I wish to use for the
I wish to use traceroute command in my java application. Unfortunately in Windows It
I wish to build a file sharing site and store the files in S3.
I wish to display my tabs (View / Edit etc.) in a sidebar and
I wish to use the same form for adding and editing records within a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.