Home > Blockchain >  Can I get the values from the "GET" parameters as array?
Can I get the values from the "GET" parameters as array?

Time:03-22

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?

CodePudding user response:

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.

CodePudding user response:

You cant have more than one parameter with the same key, You should try with http://127.0.0.1:8000/page/?key=003,004 , and then parse the value returned by key = request.GET['key'] , by splitting with a ','

  • Related