Search code examples
pythondjangogeturl-parametersparameter-list

How to get a GET request values' list in 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:

```lang-none
[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?

Solution

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