Frage

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
War es hilfreich?

Lösung

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.

Andere Tipps

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".

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top