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?

1
  • Not strictly related, but see Backbone.js it has lots of stuff to help you handle responses from and to server (models for example), and also see their TODO demo. Commented Mar 10, 2012 at 18:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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:
Thats because serializes can only serialize querysets. Use import json and then json.dumps({'key':value}).
Gah. Serves me right for trying to be fancy and use something I'd never used directly :-/
Thanks for the help, all. Any suggestions for a related question? stackoverflow.com/questions/9649820/…
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.