0

If I make a request like so:

$.ajax({
  url: '/some/url/',
  data: {
    normal_list: [1,2,3],
    dict_list: [{foo:'a'}, {bar:'b'}]
  }
})

... then I can't seem to get dict_list out of request.GET using getlist. Here's what I see when I set up a breakpoint:

ipdb> request.GET
<QueryDict: {u'dict_list[1][bar]': [u'b'], u'normal_list[]': [u'1', u'2', u'3'], u'dict_list[0][foo]': [u'a']}>
ipdb> request.GET.getlist('normal_list[]')
[u'1', u'2', u'3']
ipdb> request.GET.getlist('dict_list[]')
[]

How do I get the correct value of dict_list out of this request in django?

Edit: I am able to get it to work with dict_list: JSON.stringify([{foo:'a'}, {bar:'b'}]), but I'd like to be able to send data in a way that is agnostic of the contents.

Charles
  • 50,943
  • 13
  • 104
  • 142
hughes
  • 5,595
  • 3
  • 39
  • 55

1 Answers1

0
 $.ajax({
    type: "POST",
    url: '/some/url/',
    data: {
       'normal_list[]': [1,2,3],
       'dict_list[]': [{foo:'a'}, {bar:'b'}],
       'csrfmiddlewaretoken': '{{csrf_token}}'
    }
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function(data) { 
       //Message here or show data
    },
    error: function(ts) { 
        //Message here
        alert(ts);         
    }
 })
catherine
  • 22,492
  • 12
  • 61
  • 85