[Answered ]-POST request using ajax in Django

2👍

For Js:

$("form").submit(function() {
   $.ajax({
       url: '{% url home %}',
       data: {selected_folders: formData,
           csrfmiddlewaretoken: $("[name = csrfmiddlewaretoken]"),
        dataType: "json",
        type: "POST",
    });
});

For view.py:

def home(request):        
if request.method == 'POST' and request.is_ajax():
    Call_Other_Class()
return render_to_response('home.html')

0👍

The best solution is using the online documentation.
From what I recall:

  • first call a GET in Ajax and in the answer, force ensure_csrf_cookie decorator
  • then keep the CSRF cookie, you have all the detail explanation here.

Leave a comment