【问题标题】:displaying python list of lists in a jinja template在 jinja 模板中显示 python 列表列表
【发布时间】:2016-03-30 17:05:11
【问题描述】:

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

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

 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]]

神社模板:

<!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”字样。

【问题讨论】:

  • 我认为你只需要在 for 循环中将 val 替换为 {{val}} 即可。
  • 并同样将&lt;p&gt;triple&lt;/p&gt; 替换为&lt;p&gt;{{ triple }}&lt;/p&gt;(我认为这只是为了调试)

标签: python google-app-engine jinja2


【解决方案1】:

只是模板代码中的错字 你应该使用&lt;td&gt;{{val}}&lt;/td&gt; 而不是&lt;td&gt;val&lt;/td&gt;

【讨论】:

    猜你喜欢
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多