Tuesday, December 20, 2011

POST with a list in django

In your template, you may want to do a post with a list of values, like this:
$.post('{% url affiliate.views.manage_access %}', {
    id_list: [1,2,3],
  });
You can't just grab the value from the post like you normally would, since it will only grab one of the items.  So this won't work:
def manage_access(request):
    id_list = request.POST['id_list']
You must instead do this:
def manage_access(request):
    id_list = request.POST.getlist('id_list[]')