Skip to content

Instantly share code, notes, and snippets.

@georgefs
Created April 19, 2013 23:39
Show Gist options
  • Save georgefs/5423989 to your computer and use it in GitHub Desktop.
Save georgefs/5423989 to your computer and use it in GitHub Desktop.
django view json decoder auto warp status & errormsg
def json_api(func):
def deco(*args, **kwargs):
result = {}
try:
result['status'] = 0
result['msg'] = "success"
result['data'] = func(*args, **kwargs)
except Exception,e:
result['status'] = e.errorno if hasattr(e, 'errorno') else 1
result['msg'] = e.message
finally:
result = json.dumps(result)
return HttpResponse(result, content_type="application/json")
return deco
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment