Skip to content
michaelSaunders edited this page Mar 1, 2018 · 5 revisions

Django Notes

Sending JSON as response in view.py

from django.http import HttpResponse  # HttpResponse is how we send
import json                           # Converts to json

def [function](request):
        # Not actually JSON in this form!
	some_json = {
		'var1': 'foo',
		'var2': 'bar',
	}
        # Conversion
	data = json.dumps(some_json)
        # Return JSON and let it know its json
	return HttpResponse(data, content_type='application/json')
	

Clone this wiki locally