Javascript – In Django, I know how to build a template. render_to_response(‘something.html’). But, how to include a JSON object in there

djangojavascriptjsonobjecttemplates

When the page loads, I have a JavaScript function that needs to analyze the JSON. But, this JSON must be present when I load "something.html"

I know how to do this, but I don't know how to combine them together.

return HttpResponse(thejson, mimetype="application/javascript")

Best Solution

If you want the json to be in your template from the start, just pass it in like any other variable:

return render_to_response('something.html', {'json':thejson, 'othervariable':foo})
Related Question