[Answer]-POST.getlist() processing

1👍

Firstly, please drop the [] in the field names. That’s a PHP-ism that has no place in Django.

Secondly, if you want related items grouped together, you’ll need to change your form. You need to give each field a separate name:

<input type="text" name="client_1" value="client1" />
<input type="text" name="address_1" value="address1" />
<input type="text" name="post_1" value="post1" />
...
<input type="text" name="client_n" value="clientn" />
<input type="text" name="address_n" value="addressn" />
<input type="text" name="post_n" value="postn" />

Now request.POST will contain a separate entry for each field, and you can iterate through:

for i in range(1, n+1):
    client = request.POST['client_%s' % i]
    address = request.POST['address_%s' % i]
    post = request.POST['post_%s' % i]
    ... do something with these values ...

Now at this point, you probably want to look at model formsets, which can generate exactly this set of forms and create the relevant objects from the POST.

Leave a comment