10
votes

I have a certain link to a url in a Django template. I would like to grab all the GET parameters of the current page url and add them to the url of the template's link. The current page might have zero GET parameters.

3

3 Answers

13
votes

Include the django.core.context_processors.request context processor in your settings.py, then use the request object in your template's links:

<a href="{% url 'my_url' %}?{{ request.META.QUERY_STRING }}">

This will cause links from a page without any GET variables to have a trailing ? but that's harmless. If that's not acceptable, you could test for them first:

<a href="{% url 'my_url' %}{% if request.META.QUERY_STRING %}?{{ request.META.QUERY_STRING }}{% endif %}">
3
votes

you could pass request.META['QUERY_STRING'] to the template.

You can grab the get params before you render the template and pass them to the template and render them on the correct link.

You could also build a query string from request.GET

1
votes

The GET parameters of the current request are stored in HTTPRequest.Get.