'Can I get the values from the "GET" parameters as array?

I can get data from url like this.

http://127.0.0.1:8000/page/?key=003

I show output as Json. This is view.py

def page(request):

    key = request.GET['key']

    data=Device.objects.get(key=key)
    print(key)
   
    data = {
        "open": data.open,
        "close": data.close,
    }

    return JsonResponse(data, safe=False)

I try to get many value in same time like this

http://127.0.0.1:8000/page/?key=003&key=004

In terminal it show output like this.

[22/Mar/2022 15:19:22] "GET /page/?key=003&key004 HTTP/1.1" 200 64
003

The output show 003 only. Can I get the values from the "GET" parameters as array?



Solution 1:[1]

Yes, you can have more than one parameter with the same key. Just do this:

key = request.GET.getlist('key')

And you will get an array with the key values.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LaCharcaSoftware