Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

297
Views
Pass Django list as argument to Javascript

I have a list passed through the context into the html page of a Django project which I want to read inside of a .js which contains chartjs code. The problem is that .js is reading as string and not as a list/array.

views.py

def index(request):
    context = {
        "data": [1, 2, 3, 4, 5]}
    return render(request, 'index.html', context)

Then, in the html page I have to pass the {{ data|safe }} to a javascript like so:

<script src='{% static "js/chart.js/linechart.js" %}'
   var label = "fakelabel";
   var index = {{ data|safe }};
></script>

Inside the .js I'm reading the 'index' input as:

document.currentScript.getAttribute("index")

How can I fix this? ('label' is read correctly as str indeed).

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

{{ data|safe }} is not a safe way to output a Python object as JavaScript.

Use json_script if you want to be safe

This is how to do it.

Write your object as json:


data = {'numbers':['1', '2', '3', '4', '5']}

def index(request):
    context = {"data": data}
    return render(request, 'index.html', context)
<script>
   var index = {{ data|json_script:'mydata' }};
<script>

Then you can use the index variable into another scrip like this:

  <script>
    const data = JSON.parse(document.getElementById('mydata').textContent);
    mydata = data['numbers'];
  </script>

You could (and probably should) point to an external JavaScript file rather than using embedded JavaScript. read here for more details

about 4 years ago · Juan Pablo Isaza Report

0

The correct way to fix the above problem is:

views.py

Inside Django views you need to pass each list inside the 'context' dict into json.dumps:

def index(request):
   context ={"data": json.dumps([1, 2, 3, 4, 5])}
   return render(request, 'index.html', context)

charts.html

Then, in the html script you should add {{ data|json_script:"index" }} like you you do with the 'static' tag. Then, always in your .html script you do not need to pass the 'index' list. Therefore when you call the javascript inside your .html you just need to pass:

{{ data|json_script:"index" }}
<script src='{% static "js/chart.js/linechart.js" %}'
   var label = "fakelabel";
></script>

js/chart.js/linechart.js

Finally, inside you separate javascript (in this case "js/chart.js/linechart.js"), we can fetch the list with:

JSON.parse(document.getElementById('index').textContent)
about 4 years ago · Juan Pablo Isaza Report

0

You can use ajax to fetch list or dict data from views and you need to add jQuery cdn in html file.

from django.http import JsonResponse
def indexAjax(request):
   context={"data":[1,2,3,4,5]}
   return JsonResponse(context,safe=False)

Inside your js file or script tag

function fun(){
   $.ajax({
      url:"{% url'indexAjax" %},
      dataType:'json',
      type:'GET',
      success:function(res){
           console.log(res.data)
           //here you can write your chartjs
           }
    })}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!