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 discount开发者_如何学Cs %}
<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?
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')
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.
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.
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"
}
精彩评论