Django equivalent of PHP’s form value array/associative array

Check out the QueryDict documentation, particularly the usage of QueryDict.getlist(key).

Since request.POST and request.GET in the view are instances of QueryDict, you could do this:

<form action='/my/path/' method='POST'>
<input type="text" name="hi" value="heya1">
<input type="text" name="hi" value="heya2">
<input type="submit" value="Go">
</form>

Then something like this:

def mypath(request):
    if request.method == 'POST':
        greetings = request.POST.getlist('hi') # will be ['heya1','heya2']

Leave a Comment