Skip to content

Instantly share code, notes, and snippets.

@kotobukid
Created May 24, 2012 17:35
Show Gist options
  • Save kotobukid/2782966 to your computer and use it in GitHub Desktop.
Save kotobukid/2782966 to your computer and use it in GitHub Desktop.
from django return json(decorator)
#coding:utf-8
from django.http import HttpResponse
from django.utils import simplejson
def jsonize(original_func):
def decorated_func(*args, **kwargs):
if "callback" in args[0].GET:
ret = original_func(*args, **kwargs)
return HttpResponse('%s(%s)' % (args[0].GET["callback"],
simplejson.dumps(ret,
ensure_ascii=False)),
mimetype="text/javascript")
else:
ret = original_func(*args, **kwargs)
return HttpResponse(simplejson.dumps(ret, ensure_ascii=False),
mimetype="text/javascript")
return decorated_func
@kotobukid
Copy link
Author

"ret" must be serializable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment