POSTing ajax data in query-string format to Django

1. Sending query-string formatted data with jQuery.ajax()

The 'data' setting of jQuery.ajax() setting may either contain a String or a PlainObject.

In case of a string, the data is sent 'as-is'. See example 1:
$.ajax({
   type: 'POST',
   data: "test,1,2,3");

-> data will be sent as "test,1,2,3"

In case of an object, it is automatically converted to a query-string, no matter what the 'contentType' setting is. See example 2:
$.ajax({
   type: 'POST',
   data: {"test": 1, "test2"="two"});

-> data will be sent as "?test=1&test2=two"

Objects must be a set of key/value pair(s). If any value is an array, the conversion depends on the traditional setting.

If 'traditional' is set to true, the ajax() method will only be able to send shallow objects: a value can be an indexed array, but not an associative array (use json instead). The conversion looks like this (=  example 3):
$.ajax({
   type: 'POST',

   traditional: true,
   data: {"a": [1,2,3], "b": "four"});

-> data will be sent as "?a=1&a=2&a=3&b=four"

If 'traditional' is set to false (= default setting as of jQuery 1.8), complex objects can also be serialized. In this case, the keys that contain an array value are converted to an array-like notation. For example, the object from the previous example will look like this (= example 4, see the jQuery docs for more):
$.ajax({
   type: 'POST',

   traditional: false,
   data: {"a": [1,2,3], "b": "four"});
-> data will be sent as "?a%5B%5D=1&a%5B%5D=2&a%5B%5D=3&b=four"
-> this equals: ?a[]=1&a[]=2&a[]=3&b=four

2. Django automatically parses query-string data

Django will try to parse the input data and use it to populate the HttpRequest.GET or HttpRequest.POST object. These are dictionary-like objects containing all given parameters passed through the GET url or POST body.

Note that the contentType parameter must be set to application/x-www-form-urlencoded' (= the default). If you explicitly set another contentType, django will not populate the HttpRequest.GET or HttpRequest.POST objects.

If the data is a string, django will create a QueryDict with a single key (= the string) and a list containing an empty string as value. Example 1:
-> HttpRequest.POST = <QueryDict: {u'test,1,2,3': [u'']}>

In case of a simple object (no array values), keys and values will correspond to the ones that were sent. Integer values are converted to unicode strings. Example 2:
-> HttpRequest.POST = <QueryDict: {u'test': u'1', u'test2': u'two'}>

In case the object has at least one array value, converted with traditional set to true, all QueryDict values are converted to arrays (of which the content corresponds to the javascript object, with integer values being converted to unicode strings).
Example 3:
$.ajax({
   type: 'POST',

   traditional: true,
   data: {"a": [1,2,3], "b": "four"});

-> data will be sent as "?a=1&a=2&a=3&b=four"
-> django will store the input data in HttpRequest.POST as:
<QueryDict: {u'a': [u'1', u'2', u'3'], u'b': [u'four']}>

In case the object has at least one array value, converted with traditional set to false, django's QueryDict keys will also have the array-like notation. Example 4:
<QueryDict: {u'a[]': [u'1', u'2', u'3'], u'b': [u'four']}>

The QueryDict object has a getlist() method, which must be used to retrieve an array value as a python list.

3. Don't forget to send the csrf_token

If you're POSTing a query-string formatted object with jQuery.ajax(), you could include a 'csrfmiddlewaretoken'  key as part of the object (containing the actual token string as its value). Django will automatically find and process it, so you don't have to set the custom X-CSRFToken header.

$.ajax({
   type: 'POST',
   data: {"test": 1, "csrfmiddlewaretoken":"kigLe2yn9pgaDZjkl9fsk7vZoAW7OkrU"});

-> HttpRequest.POST = test': u'1', u'csrfmiddlewaretoken': 'kigLe2yn9pgaDZjkl9fsk7vZoAW7OkrU'}>

Comments

Popular posts from this blog

Handling control characters (escaping) in python for json and mysql

python port sniffer with pcapy and impacket

Django field, form and model validation process