0

How can I send the variable list to Django in the code below?

var list = [];
function add_item(item,next){
  list.push(item.name);
  item.parentNode.style.display = "none";
  next.style.display = "block";
  console.log(list);  }
function remove_item(item,prev){
  for (var i = 0; i <= list.length; i++) {
    if (list[i]===item.name) {
      list.splice(i,1);
    }  }
  item.parentNode.style.display = "none";
  prev.style.display = "block";
  }
$(document).ready(function() {
  $.ajax({
      method: 'POST',
      url: '/food_output',
      data: {'list': list},
      success: function (data) {
           //this gets called when server returns an OK response
           alert("it worked!");
      },
      error: function (data) {
           alert("it didnt work");
      }
  });
});
2
  • you should paste your django code here.that other people need to know how to answer your question. Commented Jun 15, 2021 at 14:57
  • this will help you guys it worked for me stackoverflow.com/a/34951258/15527968 Commented Jun 16, 2021 at 7:00

2 Answers 2

1

I use Django REST Framework here. This solution can submit more complex data to the server. If you use a modern library such as axios, you don't even need to use JSON.stringify()

$(document).ready(function() {
    list = [1,2,3,4]
    $.ajax({
        method: 'POST',
        url: '/food_output',
        contentType:"application/json",
        data: JSON.stringify({'list': list}),
        success: function (data) {
            //this gets called when server returns an OK response
            alert("it worked!");
        },
        error: function (data) {
            alert("it didnt work");
        }
    });
});
from django.http import JsonResponse
from rest_framework.decorators import api_view

@api_view(['POST'])
def food_output(request):
    print(request.data['list'])
    return JsonResponse({'success':True})

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

First join the list to string; then, on Django view, split it using comma (,)

$(document).ready(function() {
  $.ajax({
      method: 'POST',
      url: '/food_output',
      data: {'list': list.join(",")},
      success: function (data) {
           //this gets called when server returns an OK response
           alert("it worked!");
      },
      error: function (data) {
           alert("it didnt work");
      }
  });
});

views.py

def your_method(request):
    list = request.POST.get("list")
    list = lists.split(",")

5 Comments

why not just serialize the list like described here: stackoverflow.com/questions/4505871/… ?
throwing error 'NoneType' object has no attribute 'split'
and i am also not familiar with ajax. So we have to call this function from submit or not
bro ajax function is not running maybe because when i want to console.log anything it dosent shown
sorry there was a typo ,I have edited it now

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.