开发者

How to import modules into web.py templates?

I have following piece of code:

render = web.template.render('templates/')
app = web.application(urls, globals())

I read about template imports in 开发者_开发百科the web.py cookbook.

Now, when I try to import re in a template:

render = web.template.render('templates/', globals={'re':re})
app = web.application(urls, globals())

I got an error:

<type 'exceptions.TypeError'> at /'dict' object is not callable

and this line showed in the traceback: app = web.application(urls, globals()).

But when I modify it:

app = web.application(urls)

the error is gone, and re is imported in my template.

I don't understand how globals={'re': re} in web.template.render breaks it?

Why I can't keep both globals as in second example?


I'm guessing there is something else you are doing in your script or template that is causing that error. If you showed the complete example, then it would be easier to see. Here is a working example:

import web
import re

urls = ('/', 'index')
render = web.template.render('templates/', globals={'re':re})
app = web.application(urls, globals())

class index:
    def GET(self):
        args = web.input(s='')
        return render.index(args.s)

if __name__ == '__main__':
    app.run()

And the template, index.html:

$def with(s)

$code:
    if re.match('\d+', s):
        num = 'yes'
    else:
        num = 'no'

<h1>Is arg "$:s" a number? $num!</h1>

Browse to http://localhost:8080/?s=123 to try it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜