views:

104

answers:

2

I know how to use the JQuery ajax feature to call the "url view" of Django.

import simplejson as json
def the_view(request):
    fruits = {'color':5, 'type': 22}
    jfruit = json.dump(fruits)

return render_to_response( THE JSON OBJECT!!! ...how? )
+5  A: 

return HttpResponse(simplejson.dumps(mydictionary), mimetype="application/json")

see b-list

DrBloodmoney
Though really the JSON mimetype is `application/json`.
LeafStorm
Good point, I changed it. Really it's just to eliminate the XSS security problems for returning the default 'text/html' mimetype, so either will work. Thanks :)
DrBloodmoney
+1  A: 

Or shorter: download http://bitbucket.org/offline/django-annoying/ and write:

@ajax_request 
def the_view(request):
    return {'color':5, 'type': 22}

There are a few such nice tiny things in django-annoying.

Tomasz Zielinski