Skip to content

Instantly share code, notes, and snippets.

@amelieykw
Last active July 19, 2018 09:43
Show Gist options
  • Select an option

  • Save amelieykw/4069f5e9151ea95219ec82b88ad06cd3 to your computer and use it in GitHub Desktop.

Select an option

Save amelieykw/4069f5e9151ea95219ec82b88ad06cd3 to your computer and use it in GitHub Desktop.
[Django - JsonResponse] #Django #JsonResponse

I usually use a dictionary, not a list to return JSON content.

import json

from django.http import HttpResponse

response_data = {}
response_data['result'] = 'error'
response_data['message'] = 'Some error message'

Pre-Django 1.7 you'd return it like this:

return HttpResponse(json.dumps(response_data), content_type="application/json")

For Django 1.7+, use JsonResponse as shown in this SO answer like so :

from django.http import JsonResponse
return JsonResponse({'foo':'bar'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment