52👍
Don’t send it in that format in the first place. The standard way of sending multiple values for a single HTML is to send the parameter multiple times:
http://127.0.0.1:8000/auction/?status=omn&status=aad
which will correctly give you ['omn','aad']
when you use request.GET.getlist('status')
.
6👍
Expanding on @DanielRoseman’s answer.
The correct way would be to pass each variable as described: http://127.0.0.1:8000/auction/?status=omn&status=aad
.
However, if you’re using modern Javascript frameworks (Vue, Angular, React) there’s a good chance you’re passing params as an object (e.g., if you’re working with axios, VueResource, etc). So, this is the work around:
Front-end:
let params = {
status: ['omn', 'aad',]
}
return new Promise((resolve, reject) => {
axios.get(`/auction/`, { params: params }, }).then(response => {
resolve(response.data);
}).catch(error => {
resolve(error.response);
});
});
This will then dispatch to Django as the following URL:
[05/Aug/2019 10:04:42] "GET /auction/?status[]=omn&status[]=aad HTTP/1.1" 200 2418
Which can then be picked up in the corresponding view as:
# Before constructing **parameters, it may neccessary to filter out any supurfluous key, value pair that do not correspond to model attributes:
parameters['status__in'] = request.GET.getlist('status[]')
# Using parameters constructed above, filter the Auctions for given status:
auctions = Auction.objects.filter(is_active=True)
auctions = auctions.filter(**parameters)
- [Django]-Effects of changing Django's SECRET_KEY
- [Django]-How can I fill up form with model object data?
- [Django]-What is the different between save(), create() and update () in django rest framework?
- [Django]-Is there a built-in login template in Django?
- [Django]-How can one use enums as a choice field in a Django model?
- [Django]-Django using get_user_model vs settings.AUTH_USER_MODEL
1👍
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 #}
- [Django]-Getting 'str' object has no attribute 'get' in Django
- [Django]-"No module named simple" error in Django
- [Django]-Django: Display current locale in a template
-4👍
Here is the documentation of HTTPRequest and HTTPResponse
- [Django]-How to suppress the deprecation warnings in Django?
- [Django]-Update to Django 1.8 – AttributeError: django.test.TestCase has no attribute 'cls_atomics'
- [Django]-How to produce a 303 Http Response in Django?