For example, if you access the url below:
https://example.com/?fruits=apple&fruits=orange
Then, you can get the GET
request values' list in views.py
as shown below. *My answer explains how to get a POST
request values' list in Django and my answer explains how to get GET
request values in Django:
# "views.py"
from django.shortcuts import render
def index(request):
print(request.GET.getlist('fruits')) # ['apple', 'orange']
print(request.GET.getlist('meat')) # []
print(request.GET.getlist('meat', "Doesn't exist")) # Doesn't exist
print(request.GET._getlist('fruits')) # ['apple', 'orange']
print(request.GET._getlist('meat')) # []
print(request.GET._getlist('meat', "Doesn't exist")) # Doesn't exist
print(list(request.GET.lists())) # [('fruits', ['apple', 'orange'])]
print(dict(request.GET)) # {'fruits': ['apple', 'orange']}
print(request.META['QUERY_STRING']) # fruits=apple&fruits=orange
print(request.META.get('QUERY_STRING')) # fruits=apple&fruits=orange
return render(request, 'index.html')
Then, you can get the GET
request values' list in index.html
as shown below:
{# "index.html" #}
{{ request.META.QUERY_STRING }} {# fruits=apple&fruits=orange #}
In addition, if you access the url below:
https://example.com/?fruits=apple,orange
Then, you can get the GET
request values' list in views.py
as shown below:
# "views.py"
from django.shortcuts import render
def index(request):
print(request.GET['fruits'].split(',')) # ['apple', 'orange']
print(request.GET.getlist('fruits')[0].split(',')) # ['apple', 'orange']
print(request.GET._getlist('fruits')[0].split(',')) # ['apple', 'orange']
print(list(request.GET.values())[0].split(',')) # ['apple', 'orange']
print(list(request.GET.items())[0][1].split(',')) # ['apple', 'orange']
print(list(request.GET.lists())[0][1][0].split(',')) # ['apple', 'orange']
print(request.GET.dict()['fruits'].split(',')) # ['apple', 'orange']
print(dict(request.GET)['fruits'][0].split(',')) # ['apple', 'orange']
print(request.META['QUERY_STRING']) # fruits=apple,orange
print(request.META.get('QUERY_STRING')) # fruits=apple,orange
return render(request, 'index.html')
Then, you can get the GET
request values' list in index.html
as shown below:
{# "index.html" #}
{{ request.GET.fruits }} {# apple,orange #}
{{ request.GET.dict }} {# {'fruits': 'apple,orange'} #}
{{ request.META.QUERY_STRING }} {# fruits=apple,orange #}
/auction/?status[]=omn&status[]=aad
, so you'd then need to access the key values asrequest.GET.getlist('status[]')
. – Bingle