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

标签 python web.py

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

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

$def 与 (num)

$(str(数字)) 或者 $str(数字)

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

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

最佳答案

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

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

关于python - 在 web.py 模板 : global name not found 中使用内置 'str' 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4557635/

相关文章:

Python:python3 上 web.py 的替代方案

Python ElementTree 模块 : How to ignore the namespace of XML files to locate matching element when using the method "find", "findall"

python - 在python中重复一个变量

python - 使用和不使用基于目标的编码的流水线

python - 使用 web.py 呈现多个模板

python - Gevent joinall 阻塞错误

python - 如何在 web.py python 中的浏览器上显示验证消息

python - 用给定范围内的随机值填充一个 numpy 数组?

python - 导入 matplotlib 时出错,除非使用 anaconda

python - 如何在不依赖框架的情况下使用 python 进行 Web 开发?