finspin
finspin

Reputation: 4061

Passing list of values to django view via jQuery ajax call

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.

Upvotes: 18

Views: 36622

Answers (5)

Pablo Abdelhay
Pablo Abdelhay

Reputation: 1008

You can access this array by request.POST.getlist('terid[]') in the view

in javascript:

$.post(postUrl, {terid: values}, function(response){
    alert(response);
});

in view.py:

request.POST.getlist('terid[]')

It works perfect for me.

Upvotes: 49

finspin
finspin

Reputation: 4061

I found a solution to my original problem. Posting it here as an answer, hopefully it helps somebody.

jQuery:

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

/ingredients/ view:

def ingredients(request):
    if request.is_ajax():
        ourid = json.loads(request.raw_post_data)
        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)

Upvotes: 12

toto_tico
toto_tico

Reputation: 19027

It seems you are setting the array to string here

data: {'terid': values},

It should be

data: {terid: values}

Upvotes: 1

Josh Smeaton
Josh Smeaton

Reputation: 48720

This part is your problem:

ourid = request.POST.get('terid', False)
ingredients = Ingredience.objects.filter(food__id__in=ourid)

You need to deserialize the JSON string.

import json
ourid = json.loads(request.POST.get('terid'))

Upvotes: 3

gabberr
gabberr

Reputation: 357

Try sending data like this:

   data: values;

Upvotes: 0

Related Questions