ViTo Brothers ViTo Brothers - 1 year ago 135
Python Question

How to add the current query string to an URL in a Django template?

When I load a page, there is a link "sameLink" that I want to append to it the query string of its containing page.

I have following URL:


How can I do that?

Answer Source

To capture the QUERY_PARAMS that were part of the request, you reference the dict that contains those parameters (request.GET) and urlencode them so they are acceptable as part of an href. request.GET.urlencode returns a string that looks like ds=&date_published__year=2008 which you can put into a link on the page like so:

<a href="sameLink/?{{ request.GET.urlencode }}">