0

I'm confused about how to do it via Ajax or Json, but how can I send the selection array (curCheck) on-click to Django views and receive it as a python array

javascript

document.getElementById('results').addEventListener('click', function(){
    html_table = '<thead><tr><th>Currency</th><th>Amount</th><th>Symbol</th>><tr/><thead/>'
        var checkElements = document.getElementsByClassName('ch');
        for(var i =0; i< curname.length; i++){
            if (checkElements[i].checked) {
            var curChecked = curname[i];
            var JsonArr = JSON.stringify(curChecked);
            postcurChecked(JsonArr)
            html_table += '<tr><td>' + curname[i] + '</td>';
            }
}
document.getElementById('result_table').innerHTML = html_table;
},false;

ajax

function postsubChecked(curChecked) {
    $.ajax({
        "url": "http://127.0.0.1:8000/results/",
        "type": "POST",
        "data": {"checkbox": curChecked},
        "headers": { 'X-CSRFToken': getCookie('csrftoken')}
    })
}

in django

def currencyChecked(request): 
    body_unicode = request.body.decode('utf-8') 
    body_unicode = body_unicode.replace('%22','') 
    print(body_unicode) json_data = json.loads(body_unicode.read())

I would like to see the python array print to see it is passed to the back

but I keep getting this error: json_data = json.loads(body_unicode.read()) AttributeError: 'str' object has no attribute 'read'

2
  • 1
    you don't need to use request.body and decode it, just request.POST.get('checkbox') should be the json string you want. And why read()? Just json.loads(request.POST['checkbox']) should do the trick. Commented Apr 29, 2019 at 15:47
  • @dirkgroten thank you so much! That totally worked. Learner here, that's why! :D Commented Apr 29, 2019 at 16:50

1 Answer 1

1

For getting the selected checkbox values and sending as an array using ajax you can use jquery like this:

consider you have multiple checkboxes and a button.

<input type="checkbox" name="imageName">
<input type="checkbox" name="imageName">
.......
<button id="deletePhoto">Delete</button>

after selecting multiple checkbox values click on this button. On clicking the below jquery will be triggered to make an arrya of selected checkbox values.

//jquery for getting the selelcted checkbox values 

  $(document).on("click","#deletePhoto",function(){

    var favorite = [];//define array

    $.each($("input[name='imageName']:checked"), function(){            
        favorite.push($(this).val());
    });

    alert("Photo Selected: " + favorite.join(", "));

    if(favorite.length == 0){
      alert("Select Photo to delete");
      return false;
    }

    //ajax for deleting the multiple selelcted photos
    $.ajax({type: "GET",
    url: "/olx/deletePhotoFromEdit",
    data:{
      favorite:favorite
    },
    success: function(){
     // put more stuff here as per your requirement
    });

   }
    });

});

In the view you can get the array like this:

selected_photo = request.GET.getlist('favorite[]')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.