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 #}
- [Django]-Django-social-auth django-registration and django-profiles β together
- [Django]-Django templates: verbose version of a choice
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
Source:stackexchange.com