views:

297

answers:

4

I have a form with this inputs:

<input name="person[name]" value="">
<input name="person[surname]" value="">
<input name="person[age]" value="">

when I submit, how can i assign that html array to a variable, cause request.POST.getlist('person') doesn't work, i been checking for other post but the only one i found doesn't have anything usefull

THEAD

I hope someone could help me figure it out, cause a read the doc, and did quite get the way to do it...

+1  A: 

this doesn't seem like a very pythonic way to do it. or even a django-nic way to do it.

http://docs.djangoproject.com/en/dev/topics/forms/

I haven't really done a lot of forms stuff with django yet, but this looks like it would be helpful in terms of automatic generation, validation, etc.

Oren Mazor
+1  A: 

If you define your forms this way in templates, you cannot map it to a dictionary directly.

You should obtain individual values only

request.POST['person[name]']

However, this is no way to use forms in django. You should rather define these fields as per django form declarative syntax (docs), and let django handle the rendering in the templates using a tag like:

{{form.as_p}}
{{form.as_table}}

That way, you can define save method on the form class to perform your "array mapping" function. If you want to map it to a model defined, this comes stocked, and your form should extend ModelForm, to take that advantage.

Lakshman Prasad
the thing is that i have a table in my db with discounts... where every discount has a default value...so i made it{% for i in discounts %}<input name="discount[{{ i.id }}]" value="{{ i.default_value }}">and on my view i dont have any method to catch that html array i'm sending... any suggestions?
i-Malignus
You should create a modelform, or a form and pass the initial dictionary to it, as appropriate. docs referred to above can help. If you need help in anything specific, ask a separate question, or update this one.
Lakshman Prasad
A: 

the thing is that i have a table in my db with discounts... where every discount has a default value... so i made it

discounts = Discount.objects.all( )

{% for i in discounts %} 
<input name="discount[{{ i.id }}]" value="{{ i.default_value }}"> 
{% endfor %}

and on my i dont have any method to catch that html array i'm sending... any suggestions?

i-Malignus
A: 

Sorry answering such old question, but i stuck on the same problem and did not found any acceptable answers on it. So here is my solution:

def get_post_dict(post, key):
    result = {}
    if post:
        import re
        patt = re.compile('^([a-zA-Z_]\w+)\[([a-zA-Z_\-][\w\-]*)\]$')
        for post_name, value in post.items():
            value = post[post_name]
            match = patt.match(post_name)
            if not match or not value:
                continue
            name = match.group(1)
            if name == key:
                k = match.group(2)
                result.update({k:value})
    return result

Now you can use it like this:

persons = get_post_dict(request.POST, 'person')
...

or

django.http.QueryDict.getdict = get_post_dict
persons = request.POST.getdict('person')
Sergey