Skip to content Skip to sidebar Skip to footer

Django Ajax Request Only Getting Last Element (not Getlist Issue)

I'm trying to make a tagging system in Django. Basically I'm passing through a list of tags (checkboxes in a form) through AJAX to a Django view which will update the list of tags

Solution 1:

As suggested by Daniel Roseman:

Instead of passing a list, I used the join function in the two variables into a comma delineated string:

var all_tags = $("input:checkbox").map(function() { returnthis.id; }).get();
var selected_tags = $("input:checkbox:checked").map(function() { returnthis.id; }).get();

From there, I used the split function in Django to reverse the process:

all_tags = request.GET.getlist("all_tags")[0].split(",")

Not the most direct way of solving the problem, but quick and easy.

Solution 2:

The problem is that jQuery is serializing the array by using duplicate query parameters, something that Amazon API Gateway (used by Zappa) doesn't support.

Or didn't, I should say. Just a few days ago Amazon announced that API Gateway will now support this:

Starting today, Amazon API Gateway supports multiple headers and query string parameters with the same name in the API Request.

In any case, as you've discovered, you can just implement your own serialization that doesn't require duplicate query parameters.

Post a Comment for "Django Ajax Request Only Getting Last Element (not Getlist Issue)"