不使用add_static_view添加引用

时间:2015-01-31 00:09:44

标签: python pyramid

我正在Pyramid中构建应用程序并使用Jinja2模板和遍历路由。为了使用我正在使用的模板连接我的视图 - callables,我希望能够使用webapp:templates前缀引用我的模板。举个例子:

@view_config(name='about-us', renderer='webapp:templates/pages/about-us.html', context=Root)
def static_pages(context, request):
    ... //more code

这解决了模板所处的位置。但是为了实现上述功能,我必须将它放在我的webapp根文件夹中的__init__.py内:

config.add_static_view(name='templates', path='webapp:templates', cache_max_age=3600)

add_static_view()导致webapp/templates文件夹在其他配置中被引用为webapp:template。但是,它也可以通过http://0.0.0.0:6543/templates/<some template file>等网址查看。有没有办法实现前一个目标,而不允许后者作为静态页面的可见性?

1 个答案:

答案 0 :(得分:2)

add_static_view() 不应该导致webapp/templates文件夹在其他配置中被引用为webapp:template,如果这样做,它只是由于奇怪的副作用。

package:path语法有效,因为Pyramid使用pkg_resources API来解析路径。以下是一些details

这意味着,在您的示例中,webapp应该是位于您的应用可以找到它的某个位置的python包。