I'm using Django's render_to_response to write data out to an html page, but I'd also like that render_ to _response call to load a python dictionary into a javascript associative array. What's the best way to do this?
3 Answers
What does that mean exactly? If you mean you think data in the template is in JavaScript terms, it isn't: You can use python objects in the template directly. If you mean, how do I embed a JSON literal from a Python dictionary or list into my template: Encode it with simplejson, which is included with Django.
But, you often don't want to do this for a couple reasons. If you include this dynamic data in the template, you can't cache it as easily. Shouldn't this be another view that is generating a JS file you're including? Or maybe an AJAX call to grab the data once the page is live? Take the pick for what best fits you situation.