Work JSON data with Django

0

When the convertir() method is executed, all data in the table is converted to JSON , but I need to get the data for each row instead of the entire table. How can I do that?

Javascript Code:

function convertir() {
    var table = $('#tabla_json').tableToJSON({
    ignoreColumns: [0]
});
    return table;
}
$(document).ready(function(){

    //Ajax Post
    $('#aceptar').click(function(){
        $.ajax({
            type: "POST",
            url: "/fondo_habitacional/guardar_tabla/",
            dataType: "json",
            data: {"table": JSON.stringify(convertir())},
            success: function(data){
                alert(JSON.stringify(data));
            }
        });
    });
});

In the views ( View ):

def guardar_tabla(request):
    if request.is_ajax() and request.POST:
        data = request.POST.get('table')
        return HttpResponse(json.dumps(data), content_type='application/json')
    else:
        raise Http404

Table:

This is the table which I enter the data, what I need is to save the data that I enter in this table to the database .. For this the function converts me to json and with ajax I owe it to send to the Database ..

    
asked by glorenzo 13.05.2017 в 15:42
source

1 answer

1

In Script:

function convertir() {
    var table = $('#tabla_json').tableToJSON({
    ignoreColumns: [0]
});
    return table;
}


 $(document).ready(function(){

    //Ajax Post
    $('#aceptar').click(function(){
       $.ajax({
           type: "POST",
           url: "/fondo_habitacional/guardar_tabla/",
           dataType: "json",
           data: {"table": JSON.stringify(convertir())},
           success: function(data){
               alert(JSON.stringify(data));
           }
       });
    });
});

In Views:

def guardar_tabla(request):

    if request.is_ajax() and request.POST:

        data = request.POST.get('table')

        return HttpResponse(json.dumps(data), content_type='application/json')
    else:
        raise Http404

When the convert () method is executed, all data in the table becomes Json, I need to get the data for each row, not the entire table, how can I do that?

Here I enter the data and I give the button accept that is below, then he shows me this, which are the data of the complete table:

Views : def save_table (request):

if request.is_ajax() and request.POST:
    table = request.POST.items()
    data = request.POST.get('table')


    return HttpResponse(json.dumps(data), content_type='application/json')
else:
    raise Http404

script : function convert () {     var table = $ ('# json_table'). tableToJSON ({     ignoreColumns: [0] });     return table; }

$ (document) .ready (function () {

//Ajax Post
$('#aceptar').click(function(){
   $.ajax({
       type: "POST",
       url: "/fondo_habitacional/guardar_tabla/",
       dataType: "json",
       data: {"table": JSON.stringify(convertir())},
       success: function(data){
           alert(JSON.stringify(data));
       }
   });
});

});

(I'm new here, I'm not very familiar with the page, that's why I post it there and not here ..)

What I want is to save the table data in the Database ..

    
answered by 17.05.2017 в 14:50