将 Mako 与 Pyramid 结合使用会导致错误

发布于 2024-10-18 13:41:18 字数 2174 浏览 4 评论 0原文

当尝试将 Mako 与 Pyramid 框架一起使用时,通过进入development.ini 文件,并添加以下行

mako.directories = TestProject:templates 

[app:TestProject]

然后在 templates 子目录中创建一个名为 template.mako 的简单 html 文件,我得到一个:

ImportError: No module named TestProject

但我无法理解为什么这是在上述步骤之后发生的。为什么这些步骤会生成导入错误?

错误的更多详细信息:

URL: http://localhost:6543/
File 'C:\\env\\lib\\site-packages\\weberror-0.10.3-py2.7.egg\\weberror\\evalexception.py', line 431 in respond
app_iter = self.application(environ, detect_start_response)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\router.py', line 158 in __call__
response = view_callable(context, request)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\config.py', line 2839 in _rendered_view
context)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\renderers.py', line 294 in render_view
request=request)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\renderers.py', line 322 in render_to_response
result = self.render(value, system_values, request=request)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\renderers.py', line 298 in render
renderer = self.renderer
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\decorator.py', line 17 in __get__
val = self.wrapped(inst)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\renderers.py', line 280 in renderer
return factory(self)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\mako_templating.py', line 77 in renderer_factory
directories = [ abspath_from_asset_spec(d) for d in directories ]
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\asset.py', line 207 in abspath_from_asset_spec
return pkg_resources.resource_filename(pname, filename)
File 'C:\\env\\lib\\site-packages\\setuptools-0.6c12dev_r88124-py2.7.egg\\pkg_resources.py', line 881 in resource_filename
File 'C:\\env\\lib\\site-packages\\setuptools-0.6c12dev_r88124-py2.7.egg\\pkg_resources.py', line 201 in get_provider
ImportError: No module named TestProject

When trying to use Mako with the Pyramid framework, by going into the development.ini file, and adding the line

mako.directories = TestProject:templates 

in:

[app:TestProject]

and then creating a simple html file called template.mako in the templates subdirectory I get an:

ImportError: No module named TestProject

But I fail to understand why this is happening after the above steps. Why are those steps generating an importError?

More detials of the error:

URL: http://localhost:6543/
File 'C:\\env\\lib\\site-packages\\weberror-0.10.3-py2.7.egg\\weberror\\evalexception.py', line 431 in respond
app_iter = self.application(environ, detect_start_response)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\router.py', line 158 in __call__
response = view_callable(context, request)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\config.py', line 2839 in _rendered_view
context)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\renderers.py', line 294 in render_view
request=request)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\renderers.py', line 322 in render_to_response
result = self.render(value, system_values, request=request)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\renderers.py', line 298 in render
renderer = self.renderer
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\decorator.py', line 17 in __get__
val = self.wrapped(inst)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\renderers.py', line 280 in renderer
return factory(self)
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\mako_templating.py', line 77 in renderer_factory
directories = [ abspath_from_asset_spec(d) for d in directories ]
File 'C:\\env\\lib\\site-packages\\pyramid-1.0-py2.7.egg\\pyramid\\asset.py', line 207 in abspath_from_asset_spec
return pkg_resources.resource_filename(pname, filename)
File 'C:\\env\\lib\\site-packages\\setuptools-0.6c12dev_r88124-py2.7.egg\\pkg_resources.py', line 881 in resource_filename
File 'C:\\env\\lib\\site-packages\\setuptools-0.6c12dev_r88124-py2.7.egg\\pkg_resources.py', line 201 in get_provider
ImportError: No module named TestProject

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-10-25 13:41:18

摘要:

编辑文件 development.ini 并在 [app:MyProject] 部分添加:

mako.directories = myproject:模板

现在我们可以打开文件 /MyProject/myproject/__init__.py 并添加如下行:

config.add_route('foo', '/foo', view='myproject.views.foo', view_renderer='foo.mako')

这将渲染位于以下位置的模板:
/MyProject/myproject/templates/foo.mako


注意:MyProjectmyproject 是您的项目的名称。尊重案件。

请记住在 生产.ini 文件中进行相同的配置!

编辑:我在金字塔的1.3a1版本(2011-12-09)更新日志中的功能部分看到:

使用 Mako 模板不再需要 mako.directories 设置
基本原理:Mako 模板渲染器可以使用绝对值指定
资产规格整个应用程序可以用这样的方式编写
资产规格,不需要有序的查找路径。

As summary:

Edit the file development.ini and add in the section [app:MyProject]:

mako.directories = myproject:templates

Now we can open the file /MyProject/myproject/__init__.py and add a line like:

config.add_route('foo', '/foo', view='myproject.views.foo', view_renderer='foo.mako')

This will render the template located in:
/MyProject/myproject/templates/foo.mako


Note: MyProject and myproject are the name of YOUR project. Respect the case.

Remember to do the same configuration in production.ini file!

Edit:I see in the 1.3a1 version of pyramid (2011-12-09) Changelog in the Features section:

A mako.directories setting is no longer required to use Mako templates
Rationale: Mako template renderers can be specified using an absolute
asset spec. An entire application can be written with such
asset specs, requiring no ordered lookup path.

萌化 2024-10-25 13:41:18

发生这种情况是因为 mako.templates 规范的格式为 package:directory。因此,在您的情况下,您必须有一个名为“TestProject”的包。

您的项目可能有一个包,但它显然不被称为“TestProject”。

It happens because the mako.templates specifiction is in the format of package:directory. Therefore you must have a package called, in your case, "TestProject".

You may have a package for your project, but it's apparently not called "TestProject".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文