Skip to main content
 首页 » 编程设计

python之在 web.py 模板 : global name not found 中使用内置 'str' 函数

2024年09月03日39xxx_UU

我花了太多时间试图解决这个问题:

我正在向模板传递一个像这样的数字,并试图将其变成一个字符串:

$def 与 (num)

$(海峡(海峡)) 或者 $str(数)

这会生成一个错误,指出找不到全局名称“str”。

编辑和解决方案:我这样做是为了引用“img[0-n].png”。如果 num 作为数字传递,则只需说“img$(num).png”即可生成此字符串。无需将 num 显式转换为字符串。

请您参考如下方法:

将 str 作为全局变量传递给渲染对象:

render = web.template.render('templates', globals={ 'str': str })