skip to Main Content

I have an SQL request that gives me all the data I need (in Python)
I made it so that I have a list of dictionaries, it looks like that:

data = [{
    "jour_de_la_semaine": "0",
    "nom_rue": "Avenue Rogier",
    "total_lourd": 4464.676489954501,
    "total_pieton": 366.75719346780005,
    "total_velo": 1530.4361518144,
    "total_voiture": 16283.220910553997
}, {
    "jour_de_la_semaine": "1",
    "nom_rue": "Avenue Rogier",
    "total_lourd": 4689.496674622601,
    "total_pieton": 719.5071498280998,
    "total_velo": 6835.787324285102,
    "total_voiture": 22114.002369259702
}, 
..., 
...]

I put the variable in the return template so that I can use it on JavaScript.
I’m using this to do the list in JavaScript:

var data = '{{ all_data_rue|tojson }}';

I can easily see all the data with a simple console.log(data); but its impossible for me to loop through it… It always return me characters per characters.

I’ve tried every single solutions I could find on the Net. I’ve also find a post where it has the exact same problem but the solution didn’t work…

I’d like to get dictionaries per dictionaries and not characters per characters.
Any solution?

2

Answers


  1. By doing

    var data = '{{ all_data_rue|tojson }}';
    

    You are setting data to a string representation of all_data_rue. If you want to access the information from Javascript, you can do

    var data = {{ all_data_rue|tojson|safe }};
    
    Login or Signup to reply.
  2. Instead of:

    var data = '{{ all_data_rue|tojson }}';
    

    Try:

    let data = JSON.parse('{{ all_data_rue|tojson }}');
    

    or even just:

    let data = {{ all_data_rue|tojson }};
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search