Django: get data from array of objects sent by ajax formdata

1.4k Views Asked by At

I would like to access option_rows data that is sent from the front end of the web application. I use an array to store a collection of objects, include text and image, and append it into formdata, send via ajax. The console log of option_rows is as the image below.

However, I couldn't access each data in the array. How could I access each element of option_rows? Please also point out what is wrong with the code below as well. Thank for answering.

Javascript code:

var option_rows = [];
option_rows.push([{option_name : option_name,
                    answer : answer,
                    option_img : option_img}]);
var formData = new FormData();
formData.append('option_rows', option_rows);
$.ajax({
  url: "{% url 'add_question' %}",
  type: 'POST',
  data: formData,
  contentType: false,
  processData: false,
  cache: false,
  success: function(){
    $("#addModal").html("");
  },
})

option_rows log: option_rows data


views.py:

def add_question(request):
    if request.method == 'POST':
        option_rows = request.POST.get('option_rows')
        print option_rows
        print type(option_rows)

output:

Python code output

0

There are 0 best solutions below