In views.py
, I have time series data stored in a dictionary as follows:
time_series = {"timestamp1": occurrences, "timestamp2": occurrences}
where each timestamp
is in unix time and occurrences
is an integer.
Is there a way to pass the time series data as a json object in the context of the render
function?
Why do this: I am using Cal-heatmap on the front end which requires the data to be in json format. Ajax requests work just fine for now but I ideally would like to use the render
approach if possible.
If a frontend library needs a to parse JSON, you can use the
json
library to convert a python dict to a JSON valid string. Use theescapejs
filterPass a
json.dumps
value to the template. It is already a valid JSON string so you don't need to parse it or anything. Only when rendering it in the template, mark it assafe
to prevent HTML quoting.have you tried passing something like
json.dumps(time_series)
to the render function?