snakile
snakile

Reputation: 54521

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

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.

Upvotes: 10

Views: 1894

Answers (3)

dgel
dgel

Reputation: 16796

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

Upvotes: 13

dm03514
dm03514

Reputation: 55952

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

Upvotes: 3

user622367
user622367

Reputation:

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

Upvotes: 1

Related Questions