0

我有以下代码:

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

我在web.py 食谱中阅读了有关模板导入的信息。

现在,当我尝试导入re模板时:

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

我收到一个错误:

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

这条线显示在回溯中:app = web.application(urls, globals())

但是当我修改它时:

app = web.application(urls)

错误消失了,并re在我的模板中导入。

不明白怎么globals={'re': re}web.template.render

为什么我不能像第二个例子那样保留两个全局变量?

4

1 回答 1

5

我猜您在脚本或模板中正在执行的其他操作会导致该错误。如果你展示了完整的例子,那么它会更容易看到。这是一个工作示例:

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()

以及模板 index.html:

$def with(s)

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

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

浏览到http://localhost:8080/?s=123进行尝试。

于 2011-02-19T01:51:23.027 回答