[Django]-Can a Django view receive a list as parameter?

5👍

You should read about QueryDict objects:

>>> q = QueryDict('a=1&a=2&a=3')
>>> q.lists()
[('a', ['1', '2', '3'])]

2👍

Very close. The POST parameters are actually contained in a QueryDict object in the request.

def getids(request):
    if request.method == 'POST':
        for field in HttpRequest.POST:
            // Logic here
👤c_harm

2👍

If you are submitting lots of identical forms in one page you might find Formsets to be the thing you want.

You can then make one Form for the userid and then repeat it in a Formset. You can then iterate over the formset to read the results.

0👍

You can create the form fields with some prefix you could filter later.

Say you use form fields with names like uid-1, uid-2, … uid-n

Then, when you process the POST you can do:

uids = [POST[x] for x in POST.keys() if x[:3] == 'uid']

That would give you the values of the fields in the POST which start with ‘uid’ in a list.

0👍

QueryDict‘s getlist is what you’re looking for.

From the docs:

>>> q = QueryDict('a=1', mutable=True)
>>> q.update({'a': '2'})
>>> q.getlist('a')
['1', '2']

Leave a comment