在Django模板中动态访问request.GET

int*_*lis 1 python django django-templates

我正在尝试动态访问Django模板中的GET参数,但它不起作用.

网址: ?id=1&name=John

我尝试过这样的事情:

{% for r in request.GET %}
    {% if request.GET.r %}
        {{r}} = {{request.GET.r}}
    {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

问题是即使设置了参数,模板中也不会返回任何内容.

它的工作原理虽然如果我做request.GET.idrequest.GET.name

有任何想法吗?

ils*_*005 6

request.GET字典一样,你应该request.GET.items在循环中使用(docs).

{% for key, value in request.GET.items %}
    {{key}} = {{value}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)