金字塔为什么不能找到这个模板文件?

时间:2013-04-18 18:29:00

标签: python pyramid

我刚刚安装了Pyramid框架(在tutorial之后。)我创建了一个以“alchemy”scafffold(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'在目录中,但由于某种原因我仍然收到此错误。

这是 init .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

1 个答案:

答案 0 :(得分:0)

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

views.py:

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

暂时禁用金字塔debugtoolbar也有帮助。实际上你已经用这两种方式之一(docs)包含它。可以修复这些设置,也可以将其禁用以进行调试。