Skip to content

Instantly share code, notes, and snippets.

@jayapal
Created March 22, 2015 16:53
Show Gist options
  • Save jayapal/93f783d3550eaf1555ab to your computer and use it in GitHub Desktop.
Save jayapal/93f783d3550eaf1555ab to your computer and use it in GitHub Desktop.
Django listview as JSON and Jquery getJSON example
class JsonResponseMixin(object):
"""
Return json
"""
def render_to_response(self, context):
queryset = self.model.objects.all()
data = serializers.serialize('json', queryset)
return HttpResponse(data, content_type='application/json')
JSON parsing using getJSON
$.getJSON('/your/url/', function(data) {
$.each(data, function(index) {
alert(data[index].TEST1);
alert(data[index].TEST2);
});
});
// you can use http://api.jquery.com/jquery.get/
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment