在神社模板中显示 python 个列表列表

displaying python list of lists in a jinja template

我编写了一个应用程序,用户在其中输入一个数字,该应用程序计算该数字的毕达哥拉斯三元组。

我在将三元组放入列表并在 html table 中显示时遇到问题。 服务器端方法:

 def post(self):
    allTriples = []
    c = int(self.request.get('c'))
    print('c: ' + str(c))
    for i in range(1, c):
        for j in range(1, c):
            for k in range(1, c):
                i2 = i*i
                j2 = j*j
                k2 = k*k
                if ((i2 + j2) == k2) and (i < j):
                    singleTriple = []
                    singleTriple.append(i)
                    singleTriple.append(j)
                    singleTriple.append(k)
                    allTriples.append(singleTriple)
    d = {
        'allTriples' : allTriples,
    }
    print(allTriples)
    template = JINJA_ENVIRONMENT.get_template('triples.html')
    self.response.out.write(template.render(d))

这是输入 15 时列表列表的输出

[[3, 4, 5], [5, 12, 13], [6, 8, 10]]

Jinja 模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Pythagorean Triples</title>
</head>
<body>
<table>
    <th>A</th>
    <th>B</th>
    <th>C</th>
    {% for triple in allTriples %}
        <p>triple</p>
        <tr>
            {% for val in triple %}
                <td>val</td>
            {% endfor %}
        </tr>
    {% endfor %}
</table>
</body>
</html>

html 只显示单词 "val",其中数字应该是。

这只是您的模板代码中的拼写错误 你应该使用 <td>{{val}}</td> 而不是 <td>val</td>