Just a small code snippet I use when I need to return json, comments are welcomed.

from django.http import HttpResponse  
from django.utils import simplejson as json

def json_response(dict_to_convert_to_json):  
    return HttpResponse(json.dumps(dict_to_convert_to_json),
                        mimetype="application/json")

To use it in your view you just do it this way:

def login(request):  
    return json_response({'success' : True, 'user' : {'name': 'Sam',
                                                      'speciality': 'Django/Python'}})

You could also create a decorator to do this but I think using a simple function is more straight forward.


Comments

comments powered by Disqus