Generating a url with the same GET parameters as the current page in a Django template Generating a url with the same GET parameters as the current page in a Django template django django

Generating a url with the same GET parameters as the current page in a Django template


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 %}">


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


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