How to get GET request values in Django Templates?

Question:

Is it correct to say that there is no simple tag that just writes some http get
query parameter?
If all needed is printing a http get query parameter e.g. ?q=w
can I directly use the value q with a template tag or need the copy
the value in the request handler?
Is it possible to more directly pass values (all values) from http get
to template?
Because copying every value seems repeating the same handling many
times

template_values = {'q':self.request.get('q'),...

It should be possible to iterate the parameter set. Can you recommend
that or any other solution?

Asked By: Niklas Rosencrantz

||

Answers:

You don’t need to do this at all. The request is available in the template context automatically (as long as you enable the request context processor and use a RequestContext) – or you can just pass the request object directly in the context.

And request.GET is a dictionary-like object, so once you have the request you can get the GET values directly in the template:

{{ request.GET.q }}
Answered By: Daniel Roseman

For example, if you access the url below:

https://example.com/?fruits=apple&meat=beef

Then, you can get the parameters in Django Templates as shown below. *My answer explains it more:

{# "index.html" #}

{{ request.GET.fruits }} {# apple #}
{{ request.GET.meat }} {# beef #}
{{ request.GET.dict }} {# {'fruits': 'apple', 'meat': 'beef'} #}
{{ request.META.QUERY_STRING }} {# fruits=apple&meat=beef #}