Given a URL like the following, how can I parse the value of the query parameters? For example, in this case I want the value of def
.
/abc?def='ghi'
I am using Django in my environment; is there a method on the request
object that could help me?
I tried using self.request.get('def')
but it is not returning the value ghi
as I had hoped.
Python 2:
import urlparse
url = 'http://foo.appspot.com/abc?def=ghi'
parsed = urlparse.urlparse(url)
print urlparse.parse_qs(parsed.query)['def']
Python 3:
import urllib.parse as urlparse
from urllib.parse import parse_qs
url = 'http://foo.appspot.com/abc?def=ghi'
parsed = urlparse.urlparse(url)
print(parse_qs(parsed.query)['def'])
parse_qs
returns a list of values, so the above code will print ['ghi']
.