djangourl

Django parse url differently


I have an url, that contains a semicolon: /?a=3;4.

I want to make request.GET['a'] output 3;4, right now it is 3.

Is it possible?

Thank you.

Python3.8, Django 3.1


Solution

  • Yes, in django 3.1 and python 3.8 the ; character breaks the querystring.

    You have two ways to solve it:

    1. Replace it with %3B, like so /?a=3%3B4
    2. Parse the query string manually

    Example for parse the qs manually:

    from urllib.parse import parse_qs
    
    query_string = request.META['QUERY_STRING']
    params = parse_qs(query_string, keep_blank_values=True)
    a_value = params.get('a', [''])[0]