How to receive the posted values of textbox array in Django?
I have array of tex开发者_如何学Pythontboxes like this
<input type="text" value="" name="key[]" />
<input type="text" value="" name="key[]" />
<input type="text" value="" name="key[]" />
in python access the posted value as
keys = postDict['key[]']
when I print the value of the variable keys it prints only last textbox value. How to receive the posted value as list or array.
Problem
The problem is I'm appending the textbox to div with the name key when a button named MORE is clicked. Form is not posting the runtime generated textbox values.
Don't call the field key[]
. That's a PHP-ism.
Use request.POST.getlist('key')
to get all the values.
As Daniel says, use request.POST.getlist('key') to get all the values. BUT, from your comment it sounds like the name of the field you're trying to get the data for is the same for all elements, in which case .getlist() is just returning the last one it can get its hands on. (The same would happen with .get() ).
So, Django IS receiving the raw data you want, but is squashing it when you try to .get() it because of a key clash.
Is there anything stopping you using different names for each text input? If not, I would go down that route, as it's saner overall.
PS - the PHPism in your example made me think you're pretty new to Django. If this is the case, check out this great guide to the Django forms library
精彩评论