Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我花了太多时间试图弄清楚这一点:
我正在向模板传递一个这样的数字并尝试将其设为字符串:
$def 与 (num)
$(str(num)) 或 $str(num)
这会生成一个错误,提示找不到全局名称“str”。
编辑和解决方案:我这样做是为了参考“img[0-n].png”。如果 num 作为数字传递,您只需说“img$(num).png”即可创建此字符串。无需将 num 显式转换为字符串。
将 str 作为全局传递给渲染对象:
render = web.template.render('templates', globals={ 'str': str })
或者您可以使用构造“%d”将整数转换为字符串,如下所示:
stringified_int="%d" % n
或使用您的问题在模板样式中:
$ 文件名="img[0-%d]" % n
我不知道这是否适用于 python2,但适用于 python3。