[Django]-How can I easily remove an information from a url?

3👍

You can first parse the url with urlparse, and then use parse_qs to parse the querystring part:

>>> from urllib.parse import urlparse, parse_qs
>>> url = 'http://127.0.0.1:8000/accounts/dasbboard/?trxref=621538940cbc9865e63ec43857ed0f&reference=621538940cbc9865e63ec43857ed0f'
>>> parse_qs(urlparse(url).query)
{'trxref': ['621538940cbc9865e63ec43857ed0f'], 'reference': ['621538940cbc9865e63ec43857ed0f']}

This is a dictionary that maps keys to a list of values, since a key can occur multiple times. We can then retrieve the reference with:

>>> data = parse_qs(urlparse(url).query)
>>> data['reference'][0]
'621538940cbc9865e63ec43857ed0f'

Leave a comment