Ad

Get Query String Variable Name In Django

- 1 answer

I am new to Django, so my problem might be simple. I need to write a template, that would echo information from a query string. The string looks something like that: http://127.0.0.1:8000/echo/?a=1 The problem is, that query name might change from 'a' to something else, and I need to echo it accordingly. I know how to capture value from that string, but I don't know how to capture name. Please help.

def echo(request):
return render(request, 'echo.html', context={
    'get': request.GET.get('a'),
    'post': request.GET.get('b')
})


<!--DOCTYPE html -->
<html>
<body>
{% if request.method == 'GET' %}
    <h1> get a= {{ get }} statement is empty </h1>
{% elif request.method == 'POST' %}
    <h2> post b= {{ post }} statement is empty</h2>
{% endif %}
</body>
</html>
Ad

Answer

You could use HttpRequest.META

request.META['QUERY_STRING']

docs:

QUERY_STRING – The query string, as a single (unparsed) string.

Ad
source: stackoverflow.com
Ad