0

我刚刚安装了 Pyramid 框架(按照教程。我从“alchemy”脚手架(pcreate -s alchemy myproject)开始创建了一个新项目并运行了 setup.py 脚本。出于某种原因,当我运行 pserve 时,我得到了一个TopLevelLookupException,说明它找不到 uri 'pyramid_debugtoolbar:templates/exception_summary.dbtmako' 的模板:

TopLevelLookupException: Can not locate template for uri 'pyramid_debugtoolbar:templates/exception_summary.dbtmako'

文件“mytemplate.pt”在目录中,但由于某种原因,我仍然收到此错误。

这是初始化.py:

from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from .models import (
    DBSession,
    Base,
    )


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(settings=settings)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()

这是views.py:

from pyramid.response import Response
from pyramid.view import view_config

from sqlalchemy.exc import DBAPIError

from .models import (
DBSession,
MyModel,
)


@view_config(route_name='home', renderer='/templates/mytemplate.pt')
def my_view(request):
    try:
        one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()
    except DBAPIError:
        return Response(conn_err_msg, content_type='text/plain', status_int=500)
    return {'one': one, 'project': 'Triumph'}

conn_err_msg = """error"""

这是models.py文件:

from sqlalchemy import (
    Column,
    Integer,
    Text,
    )

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import (
    scoped_session,
    sessionmaker,
    )

from zope.sqlalchemy import ZopeTransactionExtension

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()


class MyModel(Base):
    __tablename__ = 'models'
    id = Column(Integer, primary_key=True)
    name = Column(Text, unique=True)
    value = Column(Integer)

    def __init__(self, name, value):
        self.name = name
        self.value = value

这是 development.ini 文件:

###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
###

[app:main]
use = egg:Triumph

pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes = pyramid_debugtoolbar

# By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'.
# debugtoolbar.hosts = 127.0.0.1 ::1

###
# wsgi server configuration
###

[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543

###
# logging configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
###

[loggers]
keys = root, triumph

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console

[logger_triumph]
level = DEBUG
handlers =
qualname = triumph

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
4

1 回答 1

0

根据官方文档(Pyramid 1.4 - Basic Layout)解释金字塔应用程序的基本布局,您需要将视图配置更改为如下所示的这一行 - 或者简单地说:从模板路径中删除前导斜杠。

视图.py:

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):

暂时禁用金字塔调试工具栏也会有所帮助。实际上,您已经将它包含在这两种方式之一(docs)中。可以修复这些设置,或者您只是出于调试目的禁​​用它。

于 2013-04-18T20:25:47.973 回答