为什么Mako无法找到与其包含的模板旁边的模板?

5 投票
1 回答
3846 浏览
提问于 2025-04-30 11:27

好的,运行这个Python代码:

from mako.lookup import TemplateLookup
from mako.template import Template
mylookup = TemplateLookup(directories=['/home/user/webapps/app/www/templates/'])
mytemplate = Template(filename='/home/user/webapps/app/www/templates/content.html.mako', lookup=mylookup)
print (mytemplate.render(title="Title", content={'hi'}))

当这段代码是content.html.mako文件的开头时:

## content.html.mako
<%inherit file="frame.html.mako"/>

我得到了这个结果:

mako.exceptions.TemplateLookupException: Cant locate template for uri '/home/user/webapps/app/www/templates/frame.html.mako'

但是frame.html.makocontent.html.mako在同一个文件夹里,怎么会这样呢?

暂无标签

1 个回答

8

在我发这个帖子后,我发现如果把第四行改成 mytemplate = mylookup.get_template('content.html.mako'),就可以正常工作了。

撰写回答