[Django]-How to get GET request values in Django Templates?

105πŸ‘

βœ…

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

1πŸ‘

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 #}

Leave a comment