0

Currently on Django and I want to do something like having an input of the same name that will hold multiple data of the same variable name. So something like:

<input type="text" name="material[{{material.id}}]" value="{{material.material_name}}">

Which will give me something like

<input type="text" name="material[1]" value="Mascara">
<input type="text" name="material[2]" value="Lipstick">
<input type="text" name="material[3]" value="Eyeliner">

And in the controller:

materials = request.POST['material']

for material in materials:
    material.log_material(material.keys.id, material.values.value)

.... something like this.

I haven't gotten that far yet but when I try to grab request.POST['material'] I get the MultiValueDictKeyError error. I can do this in PHP and am new to Python and I can't really find the right equivalent to this.

Storm Parker
  • 583
  • 3
  • 7
  • 21
  • Possible duplicate of [django MultiValueDictKeyError error, how do i deal with it](http://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it) – jDo Apr 17 '16 at 20:22
  • Try `print(request.POST.dict())` or `print(request.POST)` to see which keys are actually available. – jDo Apr 17 '16 at 20:37

1 Answers1

0

Just remove the subscripts from the field names:

<input type="text" name="material" value="Mascara">
<input type="text" name="material" value="Lipstick">
<input type="text" name="material" value="Eyeliner">

And in your view, do:

 materials = request.POST.getlist("material")
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895