0

I'm using highcharts and it requires me to pass into it variables such as this

 series: [{
        name: 'Brands',
        colorByPoint: true,
        data: [{
            name: 'Microsoft Internet Explorer',
            y: 56.33
        }, {
            name: 'Chrome',
            y: 24.03,
            sliced: true,
            selected: true
        }, {
            name: 'Firefox',
            y: 10.38
        }, {
            name: 'Safari',
            y: 4.77
        }, {
            name: 'Opera',
            y: 0.91
        }, {
            name: 'Proprietary or Undetectable',
            y: 0.2
        }]

How would I pass this into my javascript?

1
  • you can use python dictionary Commented Mar 24, 2017 at 21:56

1 Answer 1

2

You can generate the data structure in Python, and then pass it to your script as a JSON object. You need to use a library like django-argonauts to do this safely.

In your view:

data = {
    'series': [
        'name': 'Brands',
        'colorByPoint': true,
        # etc...
    ]
}

Pass this as a context variable to your template.

Then, in the template:

{% load argonauts %}
<script>
  (function () {
      var data = {{ data|json }};
      // do something with data
  })();
</script>

Where |json is a template filter provided by the Argonauts library, which will handle properly escaping all the data for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.