Using builtin 'str' function in web.py templates: global name not found
I've spent way too much time trying to figure this out:
I'm passing a template a number like so and trying to make it a string:
$def with (num)
$(str(num开发者_如何学运维)) or $str(num)
This generates an error saying global name "str" not found.
Edit & Solution: I'm doing this so I can refer to "img[0-n].png". If num is passed as a number, you can make this string by just saying "img$(num).png". No need to convert num to string explicitly.
Pass str as a global to the render object:
render = web.template.render('templates', globals={ 'str': str })
Or you can use the construct "%d" to convert ints to string, as:
stringified_int="%d" % n
or in the template style using your question:
$ filename="img[0-%d]" % n
I don't know if this works for python2, but for python3 it works.
精彩评论