2

I'm building a Django todo list. The checkboxes to mark a task as complete have ajax:

//Checkbox toggles
$('input:checkbox').click(function() {
    if ($(this).attr('checked')) {
        $action = true;
    } else {
        $action = false;
    }

    $.ajax({
        type: "POST",
        url: "/gtd/action/" + this.id.split("_")[1] + "/" + $(this).val() + "/" + $action + "/",
        success: function(data) {
            //Update entire gtd side menu
        }
    })
});

In the success portion of the ajax, I need to update multiple variables in a side menu (pertaining to the count of incomplete tasks). The django view can calculate the variables

def ajax_click(request, modelname, id, type, toggle):

    #Do some stuff to save the object

    action_count = actions = Action.objects.filter(complete=False, onhold=False).count()
    hold_count = Action.objects.filter(onhold=True, hold_criteria__isnull=False).count()

    return HttpResponse('')

The question is, how do I pass more than one variable back to the ajax function? In this instance, I have action_count and hold_count. How can I get these variables back to the success function?

Ed.
  • 4,439
  • 10
  • 60
  • 78
  • Not strictly related, but see [Backbone.js](http://documentcloud.github.com/backbone/) it has lots of stuff to help you handle responses from and to server ([models](http://documentcloud.github.com/backbone/#Model) for example), and also see their [TODO demo](http://documentcloud.github.com/backbone/examples/todos/index.html). – Davor Lucic Mar 10 '12 at 18:41

1 Answers1

5

The simplest solution is to return some JSON from the view. Something like the following:

import json
data = json.dumps({
        'actions': action_count,
        'holds': hold_count,
    })
return HttpResponse(data, content_type='application/json')

Your client-side code can then pull out the information it needs; since by the look of things you're using jQuery it can handle parsing the JSON for you automatically to pass into your success function by setting dataType: "json" in the object passed to the $.ajax() call.

Davor Lucic
  • 28,970
  • 8
  • 66
  • 76
James Aylett
  • 3,332
  • 19
  • 20
  • Thanks. I'm getting an AttributeError in django: 'str' object has no attribute '_meta'. This seems to come from C:\Python27\lib\site-packages\django-1.3.1-py2.7.egg\django\core\serializers\base.py in serialize - for field in obj._meta.local_fields: – Ed. Mar 10 '12 at 18:50
  • Thats because [serializes](https://docs.djangoproject.com/en/dev/topics/serialization/) can only serialize querysets. Use `import json` and then `json.dumps({'key':value})`. – Davor Lucic Mar 10 '12 at 19:04
  • 1
    Gah. Serves me right for trying to be fancy and use something I'd never used directly :-/ – James Aylett Mar 10 '12 at 19:13
  • Thanks for the help, all. Any suggestions for a related question? http://stackoverflow.com/questions/9649820/convert-multiple-querysets-to-json-in-django – Ed. Mar 10 '12 at 20:16
  • The same approach will work, but bear in mind that not everything can be serialised as JSON by default, so you either have to convert to strings or other simpler types directly, or write a custom `json.JSONEncoder` and pass that in to `json.dumps`. – James Aylett Mar 10 '12 at 20:44