[Fixed]-Django can't pass dictdata into javascript variable


The Django template engine’s main purpose is to take collections of data and convert them into HTML. I may be wrong, but I do not recall any builtin tags or filters in the template engine that allow for automatic expansion of python lists/dicts to JSON string values. Generally the template language revolves around iterating over collections and converting their values into properly formatted and escaped string representations.

In this case, the most expedient solution for you is to make dat a string containing the JSON text you wish to substitute into the template.

Somewhere at the time of the file:

import json

Then change the line:

dictData['dat'] = dat


dictData['dat'] = json.dumps(dat)

In which case the string variable substitution will provide the text json representation and the html should render correctly.


Leave a comment