Home > Blockchain >  Getting URL parameters in request.GET (Django)
Getting URL parameters in request.GET (Django)

Time:03-22

How can i get all of those url parameters (1, 12-18, 5,Happy birthday)? in Django

https://domain/method/?1='12-18'&5='Happy birthday'

I have tried

parameter = request.GET.get("1", "") 

but I only get 12-18.

CodePudding user response:

The second parameter is 5, so you access 'Happy birthday':

request.GET.get('5', '')

note that the strings here will contain the single quotes ('…') as content of the string. So normally this should be done without quotes.

You can get a list of key-value pairs with:

>>> dict(request.GET)
{'1': ["'12-18'"], '5': ["'Happy birthday'"]}

This will use the keys as keys of the dictionary, and maps to a list of values, since a single key can occurs multiple times in the querystring, and thus map to multiple values.

  • Related