I am trying to find a better way to send message data to a Django View function.
What I have in my jquery code looks something like this:
var name = 'Joe'; var age = 20; $.ajax({ url:"/do_something/", type: "POST", data: {name: name, age: age}, success:function(response){}, complete:function(){}, error:function (xhr, textStatus, thrownError){ alert("error doing something"); } });
Data comes in to Django in a QueryDict object:
<QueryDict: {u'name': [u'Joe'], u'age': [u'20']}>
In the view function, I can access the following values:
def do_something(request): if request.POST: name = request.POST.getlist('name')[0] age = request.POST.getlist('age')[0]
It somehow seems wrong (accessing mail data via getlist and then getting the first item in the list) as a way to transfer post data from jquery to django. Is there a better way to send data?
source share