Best way to get query string from a URL in python?

我只是一个虾纸丫 提交于 2019-11-30 05:46:52
Qasim Khan

You can make Query string using GET parameters like this

request.GET.urlencode()

This does not include the ? prefix, and it may not return the keys in the same order as in the original request.

Jon Clements

Third option:

>>> from urlparse import urlparse, parse_qs
>>> url = 'http://something.com?blah=1&x=2'
>>> urlparse(url).query
'blah=1&x=2'
>>> parse_qs(urlparse(url).query)
{'blah': ['1'], 'x': ['2']}

In Python 3+ this is available as:

from urllib.parse import parse_qs

Documentation for urllib.parse

mynameistechno

I prefer using

request.META['QUERY_STRING']

From docs:

https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.META

This does not include the ? prefix.

you can also use request.arg also

if 'next' in request.arg and 'values' in request.arg:
    next = request.arg.get('next', '')
    value = request.arg.get('value', '')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!