Skip to content

Instantly share code, notes, and snippets.

@dougvk
Created July 17, 2012 08:55
Show Gist options
  • Save dougvk/3128183 to your computer and use it in GitHub Desktop.
Save dougvk/3128183 to your computer and use it in GitHub Desktop.
Example JSON response to Server
if request.is_ajax():
if form.is_valid():
# I doubt we would ever save on an ajax POST.
goal = form.save()
# Serialize the goal in json format and send the
# newly created object back in the reponse
# have to put it in a list even if it's just one instance.
data = serializers.serialize('json', [goal,])
else:
# Since form.errors is a proxy need to create a dict from it with unicode.
data = json.dumps(dict([(k, [unicode(e) for e in v]) for k,v in form.errors.items()]))
return HttpBadRequestResponse(data, mimetype='application/json')
return HttpResponse(data, mimetype='application/json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment