Skip to content
Advertisement

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:

somedomain/reporting/article-by-month?variable1=2008

How can I do that?

Advertisement

Answer

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 }}">
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement