5👍
Two things to take note of there, first of all, JQuery is flattening your data-structure. For each element in your dictionary (that is, your arr
variable), it creates a element in the QueryDict
with a unique key.
So,
var arr = {'one': 'data1', 'two': 'data2'};
becomes
<QueryDict: {'arr[one]': 'data1', 'arr[two]': 'data2'}>
Which is very much aligned with what you are seeing. However, in your case, it doesn’t actually seem like you are populating the array with values. Both your firebug and QueryDict
output insinuates yu have a empty dictionary. I would investigate what value is in the following line of your javascript
value = $(this).val();
Also, you are using getlist()
wrong, it gives you a list associated to a key in QueryDict, it doesn’t give you a dictionary as you are expecting it to.
What you wan’t to explore is the following, first serialise your array with JSON.stringify()
as follows:
$.post("{% url 'setting-update' %}" , { 'arr': JSON.stringify(arr), csrfmiddlewaretoken : '{{ csrf_token }}' });
and then convert it to a python dict in the view by
import json
yourdict = json.loads(request.POST.get('arr'))
1👍
you need to serialize your object to json string first:
$.post("{% url 'setting-update' %}" ,
{
arr: JSON.stringify(arr), // <-----------
csrfmiddlewaretoken : '{{ csrf_token }}'
},
function(){alert('done!')}
});
then in views.py
arr = request.POST.get('arr')
dict_ = json.loads(arr)
- [Django]-Inspite of setting (null=True, blank=True) for ModelForm field, I get validation error on leaving the field blank
- [Django]-Improve django queryset performance
- [Django]-How to access specific index in list in jinja2