Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically access request.GET in Django template

I'm trying to dynamically access GET parameters in Django template, but it's not working.

URL: ?id=1&name=John

I've tried something like this:

{% for r in request.GET %}
    {% if request.GET.r %}
        {{r}} = {{request.GET.r}}
    {% endif %}
{% endfor %}

The problem is that even if the parameters are set, nothing is returned in the template.

It works though if I do request.GET.id or request.GET.name

Any ideas?

like image 807
intelis Avatar asked Dec 14 '22 08:12

intelis


1 Answers

As request.GET is a dictionary, you should use request.GET.items in the loop (docs).

{% for key, value in request.GET.items %}
    {{key}} = {{value}}
{% endfor %}
like image 175
ilse2005 Avatar answered Jan 08 '23 07:01

ilse2005