About Modifying Query String Parameters

Use a mutable QueryDict to create a URL with modified query parameters. For example, to modify the page parameter of the current page URL, you can do this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from django.http import QueryDict

next_page_url = None
if object_list.has_next():
    q = QueryDict(request.META["QUERY_STRING"], mutable=True)
    q["page"] = object_list.next_page_number()
    params = q.urlencode()

    next_page_url = "{protocol}://{host}{path}?{params}".format(
        protocol=request.scheme,
        host=request.get_host(),
        path=request.path,
        params=params,
    )

Tips and Tricks Programming Django 2.2 Django 1.11 Django 1.8