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

136
Views
Want to pass json array as django variable inside javascript

I have a json array and I want to pass this inside a javascript variable, inside the django template. I am able to access the variable inside django template {{variable}} . But not able to access this inside the javascript.This is my variable:

[
    
    {'Book ID': '1', 'Book Name': 'Challenging Times',
    
     'Category': 'Business', 'Price': '125.60'},
    
    {'Book ID': '2', 'Book Name': 'Learning JavaScript',
    
     'Category': 'Programming', 'Price': '56.00'}
    
]

I have used {{variable|safe}} inside the js but not working. Plz let me know where I am making the mistake.

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

0

You should use the json_script template filter to add your variable to the template in a format that can be loaded in your JS

{{ variable|json_script:"foo" }}

<script>
    const foo = JSON.parse(document.getElementById('foo').textContent);
</script>
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!