Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django - getlist()

Tags:

python

django

I just posted this question jQuery - passing arrays in post request, where I don't to send arrays in post request, but there is no problem in jQuery code.

The problem is with receiving the POST request in django. I did like this.

def portfolio_add(request):     ukeys = request.POST.getlist('ukeys')     ........etc....... 

But I'm getting ukeys values as u'[]'. When I checked with just request.POST I got the values as u"<QueryDict: {u'ukeys[]': [u'68c04', u'16149']}>"

So, How to get those values as a list in Django?

Thanks!

like image 999
rnk Avatar asked Jun 25 '12 13:06

rnk


People also ask

How can I get post request in Django?

Django pass POST data to view The input fields defined inside the form pass the data to the redirected URL. You can define this URL in the urls.py file. In this URLs file, you can define the function created inside the view and access the POST data as explained in the above method. You just have to use the HTTPRequest.

What is request POST in Django?

request. POST is an attribute of this request object, it's a QueryDict (much similar to a normal Python dict). It contains the HTTP POST parameters that are sent to your view.

How do you receive data from a Django form with a post request?

Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.

What is request Meta Django?

META contains all the metadata of the HTTP request that is coming to your Django server, it can contain the user agent, ip address, content type, and so on.


1 Answers

jQuery POST's arrays with the [] suffix because PHP and some web frameworks understand that convention, and re-build the array on the server-side for you automatically. Django doesn't work that way, but you should be able to access the data via:

ukeys = request.POST.getlist('ukeys[]') 
like image 74
Jakub Roztocil Avatar answered Sep 26 '22 18:09

Jakub Roztocil