Passing list of values to django view via jQuery ajax call

Posted by finspin on Stack Overflow See other posts from Stack Overflow or by finspin
Published on 2012-06-24T10:11:27Z Indexed on 2012/06/24 21:16 UTC
Read the original article Hit count: 202

Filed under:
|
|

I'm trying to pass a list of numeric values (ids) from one web page to another with jQuery ajax call. I can't figure out how to pass and read all the values in the list. I can successfully post and read 1 value but not multiple values. Here is what I have so far:

jQuery:

var postUrl = "http://localhost:8000/ingredients/";
$('li').click(function(){
    values = [1, 2];
    $.ajax({
        url: postUrl,
        type: 'POST',
        data: {'terid': values},
        traditional: true,
        dataType: 'html',
        success: function(result){
            $('#ingredients').append(result);
            }
    });       
});

/ingredients/ view:

def ingredients(request):
    if request.is_ajax():
        ourid = request.POST.get('terid', False)
        ingredients = Ingredience.objects.filter(food__id__in=ourid)
        t = get_template('ingredients.html')
        html = t.render(Context({'ingredients': ingredients,}))
        return HttpResponse(html)
    else:
        html = '<p>This is not ajax</p>'      
        return HttpResponse(html)

With Firebug I can see that POST contains both ids but probably in the wrong format (terid=1&terid=2). So my ingredients view picks up only terid=2. What am I doing wrong?

EDIT: To clarify, I need the ourid variable pass values [1, 2] to the filter in the ingredients view.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX