我在 web.py 模板模板中使用内置的“sum”函数,我收到以下错误:
global name 'sum' is not defined
源代码如下:
$if profs:
$for prof in profs:
$sum([1, 2, 3])
我可以在终端的 Python REPL 中很好地使用“sum”。
可能是什么问题?
谢谢,雅各布
在 dict 中添加函数并作为 globals 参数传递给渲染:
render = web.template.render('templates/', globals={'sum': sum})
然后在您的模板中,您可以使用它:
$def with (numbers)
<h1>Numbers add to $sum(numbers)</h1>
并非所有 python 代码都可以在模板表示法中使用,请尝试以下操作:
$if profs:
$for prof in profs:
$code:
mysum = sum([1, 2, 3])
$mysum