Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Get query parameter list

Tags:

python

django

I've an endpoint

http://127.0.0.1:8000/auction/?status=['omn','aad']

I need to get the status list, hence I do the following

print (request.GET.getlist('status'))

It returns me

[u"['omn','aad']"]

which is a list of string of list.

I then use ast.literal_eval to convert string of list to list. Is there a direct method to get the list of status?

like image 534
PythonEnthusiast Avatar asked May 07 '15 16:05

PythonEnthusiast


People also ask

How do I get all the query params in Django?

We can access the query params from the request in Django from the GET attribute of the request. To get the first or only value in a parameter simply use the get() method. To get the list of all values in a parameter use getlist() method.

How can I get query string in Django?

Django processes the query string automatically and makes its parameter/value pairs available to the view. No configuration required. The URL pattern refers to the base URL only, the query string is implicit. For normal Django style, however, you should put the id/slug in the base URL and not in the query string!

What is Query_params in Django?

query_params is a more correctly named synonym for request. GET . For clarity inside your code, we recommend using request. query_params instead of the Django's standard request.


3 Answers

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').

like image 160
Daniel Roseman Avatar answered Sep 20 '22 10:09

Daniel Roseman


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)
like image 30
Micheal J. Roberts Avatar answered Sep 22 '22 10:09

Micheal J. Roberts


request.GET['status'] would return you ['omn', 'aad'].

like image 36
HiroshiFuu Avatar answered Sep 20 '22 10:09

HiroshiFuu