Bonjour,

Je n'arrive pas à récupérer une variable passé depuis view.py vers le index.html. J'obtiens l'erreur suivante:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '['a']' from 'ind['a']'

view.py
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import random
from random import randrange
def randomData():
    resL = []
    for i in range(0, 20):
        resL.append({
            "a" : random.randint(1, 100),
            "b": random.randint(0, 1)
        })
    return resL
 
def page(request):
    resL = randomData()
    return render(request, "page/index.html", {'data': resL})
index.html
Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
{% load static %}
<html>
  <head>
    <meta charset="UTF-8">
    <title>ChartJSW3S</title>
  </head>
 
  <body>
    <h1> Page Historique </h1>
 
    <div class="container mt-5">
        <table id="example" class="table table-striped" style="width:100%">
            <thead>
                <tr>
                    <th>A</th>
                    <th>B</th>
                </tr>
            </thead>
            <tbody>
            {% for ind in data %}
                <tr>
                    <td>{{ ind['a'] }}</td>
                    <td>{{ ind['b'] }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
 
</div>
</body>
</html>