Remove session data dictionary with ajax in Django

2

I have an array with a data dictionary in my session and would like to remove according to the line that the user is clicking on a table where those values are displayed.

My view:

def deletar_servico_ou_item_selecionado(request):

    if request.is_ajax:
        linhaClicada = request.POST.get('item')
        lista = request.session['ord-serv']
        del lista[linhaClicada - 1]
        request.session['ord-serv'] = lista

    return HttpResponse("/")

My JS:

var linhaClicada = $(this).parent().index();
$.ajax({
    url: "/atendimento/deletar-servico-ou-item-selecionado/",
    type: "POST",
    data: {item: linhaClicada},
    success: function(response) {
        console.log("Success");
    },
    error: function(response) {
        console.log("Error");
    }
});

It seems that my value comes up to Django and I do not know if I should use type: "DELETE" in that case.

    
asked by anonymous 29.10.2015 / 14:14

1 answer

1

To remove the item from your list from the index, you can use list.pop, for example:

lista = [{"0":"0"},{"1":"1"},{"2":"2"},{"3":"3"},{"4":"4"},{"5":"5"}]
lista.pop(1)
# {'1': '1'}
lista
# [{'0': '0'}, {'2': '2'}, {'3': '3'}, {'4': '4'},{"5":"5"}]

If you do not need csrf_token in your request, you can include @csrf_exempt at the beginning of your function, and import from django.views.decorators.csrf import csrf_exempt . If yes, you must go along with the data you are posting. data: {csrfmiddlewaretoken: '{{csrf_token}}'}

    
29.10.2015 / 14:38