无法使用相对路径找到金字塔的变色龙渲染器模板

Pyramid's Chameleon renderer template can't be found using relative path

我是金字塔的新手。当尝试使用变色龙作为模板引擎时,它无法在使用相对路径指定时找到模板 - 它正在 env35/lib/python3.5/site-packages/pyramid/ 寻找它,其中 env35 是我创建的虚拟环境。但是,如果指定了完整路径,它将起作用。它还可以使用使用 jinja2 作为模板引擎的相对路径。 为什么我不能使用相对路径的变色龙模板?

来自手册

add_view(...., renderer,...)

This is either a single string term (e.g. json) or a string implying a path or asset specification (e.g. templates/views.pt) naming a renderer implementation. If the renderer value does not contain a dot ., the specified string will be used to look up a renderer implementation, and that renderer implementation will be used to construct a response from the view return value. If the renderer value contains a dot (.), the specified term will be treated as a path, and the filename extension of the last element in the path will be used to look up the renderer implementation, which will be passed the full path. The renderer implementation will be used to construct a response from the view return value.

Note that if the view itself returns a response (see View Callable Responses), the specified renderer implementation is never called.

When the renderer is a path, although a path is usually just a simple relative pathname (e.g. templates/foo.pt, implying that a template named "foo.pt" is in the "templates" directory relative to the directory of the current package of the Configurator), a path can be absolute, starting with a slash on UNIX or a drive letter prefix on Windows. The path can alternately be a asset specification in the form some.dotted.package_name:relative/path, making it possible to address template assets which live in a separate package.

The renderer attribute is optional. If it is not defined, the "null" renderer is assumed (no rendering is performed and the value is passed back to the upstream Pyramid machinery unmodified).

以下是我设置环境的步骤...

export VENV=~/Documents/app_projects/pyramid_tutorial/env35
python3 -m venv $VENV
source $VENV/bin/activate  #activate the virtual environment
pip install --upgrade pip
pip install pyramid 
pip install wheel
pip install pyramid_chameleon
pip install pyramid_jinja2

我的文件结构:

pyramid_tutorial
    env35
        bin
        ...
    templates
        hello.jinja2
        hello.pt
    test_app.py

test_app.py:

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config

def hello(request):
    return dict(name='Bugs Bunny')

if __name__ == '__main__':
    config = Configurator()
    config.include('pyramid_chameleon')
    config.include('pyramid_jinja2')

    #This does not work... http://localhost:6543/chameleon
    config.add_route('hello_world_1', '/chameleon')
    config.add_view(hello, route_name='hello_world_1', renderer='templates/hello.pt')
    # ValueError: Missing template asset: templates/hello.pt (/home/david/Documents/app_projects/pyramid_tutorial/env35/lib/python3.5/site-packages/pyramid/templates/hello.pt)

    #This works... http://localhost:6543/chameleon2
    config.add_route('hello_world_2', '/chameleon2')
    config.add_view(hello, route_name='hello_world_2', renderer='/home/david/Documents/app_projects/pyramid_tutorial/templates/hello.pt')

    #This works... http://localhost:6543/jinja
    config.add_route('hello_world_3', '/jinja')
    config.add_view(hello, route_name='hello_world_3', renderer='templates/hello.jinja2')

    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 6543, app)
    print ('Serving at http://127.0.0.1:6543')
    server.serve_forever()

hello.pt:

<p>Hello <strong>${name}</strong>! (Chameleon renderer)</p>

你好.jinja2:

<p>Hello <strong>{{name}}</strong>! (jinja2 renderer)</p>

指定renderer=__name__ + ':templates/hello.pt'即可。解析逻辑在这种情况下不起作用,因为文件没有作为 python 包执行,因此可能会发生一些奇怪的事情。 pyramid_chameleon 可能会在此处更新以提供更好的支持,但到目前为止,真实应用程序的常见情况是将您的代码编写为一个包,它将按预期工作。

如果您通过 python -m test_app.

稍微调整一下 运行 您的脚本作为一个模块,它也可能会起作用