Lots of jQuery and Django flags

I am starting to work in jquery, so please bear with me. I have a jquery function that allows me to select multiple checkboxes and create a string as follows:

function getSelectedVals(){
     var tmp =[];
     $("input[name='checks']").each(function() {
     if ($(this).attr('checked'))
     {
        checked = ($(this).val());
        tmp.push(checked);
     }
     });
     var filters = tmp.join(',');
     alert(filters)
     return filters;
}

Then I call the django view function and pass the line as follows:

selected = getSelectedVals();
var myurl = "/bills/delete/?id=" + selected;
$.ajax({
    type: "GET",
    url: myurl,
    data: selected,
    cache: false

});

On the server, I have a view view function that iterates over the checkbox values ​​and manipulates the list.

def delete(request):
    global myarray
    idx = request.GET[u'id']
    listidx = idx.split(',')
    for l in listidx:
        value = myarray[int(l)]
        myarray.remove(value)
    return HttpResponse("/bills/jqtut/")

The problem is that on the server, all the indexes that I send as a GET string are processed not only, but also half.

Please help me! Thanks

+3
source share
2 answers

, , . . , GET, POST.

<input type="checkbox" name="vehicle" value="Bike" />
<input type="checkbox" name="vehicle" value="Car" />
<input type="checkbox" name="vehicle" value="Airplane" />

getlist() :

def delete(request):
    values = request.POST.getlist(u'vehicle')
    # Handling goes here.

, ( ;), Django. OOTB. , JavaScript .

+7

-, , , . , http ( jQuery) .

Django getlist , [] .

Http , , , , , , .

, , , : , MultipleChoiceField CheckboxSelectMultiple :

my_field = forms.MultipleChoiceField(choices=SOME_CHOICES, widget=forms.CheckboxSelectMultiple())

.: Django ?

+2

Source: https://habr.com/ru/post/1731169/


All Articles