2

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


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?

2
  • Coming form PHP this is a rather intuitive way of operating on forms and arrays. Don't really see a downside of this sort of auto-parsing. Could easily see django allowing request.POST.get('person',{}).get('surname', '') Commented May 8, 2013 at 19:02
  • For multiple inputs with the identical name you can use request.POST.getlist(...) Commented May 9, 2013 at 4:00

4 Answers 4

4

Sorry for answering such an old question, but i stuck with the same problem and didn't found any acceptable answers for 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')
Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure that Django has not implemented such a useful thing?
1

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.

Comments

1

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.

2 Comments

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?
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.
0

Maybe try changing your markup and adding a simple middleware method to transform into a usable data structure for your views.

For instance, given this HTML markup

<input name="person:name" value="">
<input name="person:surname" value="">
<input name="person:age" value=""> 

And a middleware function like this..

def setup_post_params(get_response):
  def middleware(req):
    if req.method == 'POST':
        req.params = {}
        for key,val in req.POST.dict().items():
            if not ':' in key:
                continue
            k,v = key.split(':')
            if not k in req.params:
                req.params[k] = {}
            req.params[k][v] = val
    return get_response(req)            
return middleware

Then you can access in your view funcs w/ req.params

def my_view_func(req):
    if req.method == 'POST':
      do_something_with(req.params)

That looks like this...

person: {
  name: "John",
  surname: "Smith",
  age: "20"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.