2

Using HTML forms if you wanted to POST over multiple variables you would have to set the name of your input field to "name[]", and then when you POST['name'] you can get the information form every input field with the name "name[]". Currently I am working with Django, and implementing a view that involves a form that will POST over a variable amount of variables. When I try to implement the same method as described above in Django, I am getting a "MultiValueDictKeyError" instead. By removing the brackets in the input variables name I no longer get the error, but it will only POST over the last value, and I need it to POST over all the values.


/* In template example.html */
<form action = "{% url view_name %}" method = POST>

/* Variable amount of these inputs */
<input type = "input" name = "name[]">
<input type = "input" name = "name[]">

<input type = "submit" name = "submit" value = "Submit">
</form>

#View example located in views.py
class view_name(request):
     #Error occurs at below line
     postVar = request.POST["name"]
     

1 Answer 1

2

Try using the following:

postVar = request.POST.getlist('name[]')

You might need to remove the [] from the input html.

Sign up to request clarification or add additional context in comments.

1 Comment

You're the man, this worked perfectly and is doing exactly what I want it to do now. All I had to do was make the input variables name and the requests name the same, and then using the POST.getlist() it is returning all the variables and not just the last. Thanks!

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.