Search code examples
djangojsonsimile

Django passing JSON to Simile Timeline


I think this is less related to using Timeline and more how I'm building my JSON object.

I have a template containing a Simile Timeline widget. All is well When the data is hard coded into the template but that's no fun...

Here's how I'm building the JSON events and passing it to the view.

View

def load_timeline_events(request):
  raw_events = [{
      "title" : "Data",
      "color" :"red",
      "start" : "0020-01-01",
      "end" : "0022-01-01",
      "description" : "20 - 22"
    },
    {
      "title" : "Log",
      "color" :"blue",
      "start" : "0002-01-01",
      "end" : "0016-01-01",
      "description" : "2 - 16"
    }]
return render('timeline.html', {'EVENTS':json.dumps(raw_events)})

Template

load_events : function() {
    timeline.events.loadJSON({
        "events" : {{ EVENTS }},
        "dateTimeFormat" : "iso8601"
    }, timeline.base_uri);
},

No error returned, just a blank Timeline box.


Solution

  • Need to cast as SafeString before passing the dictionary to the template. No need to use the json module.

    Same problem as found here: Using JSON in django template

    View

    from django.utils.safestring import SafeString
    ...
    def load_timeline_events(request):
        ....
        return render('timeline.html', {'EVENTS':SafeString(raw_events)})