3

I have a bigger testcase which uses Pyramid and Mako and also Javascript. It works fine unless I put the Javascript code into a separate file. Then it fails. I have reduced the test case to the following (3 files) replacing the JS file with a simple picture I wanted to load but that fails too with the same error message:

NotImplementedError: Can't perform this operation for unregistered loader type
127.0.0.1 - - [15/Oct/2021 10:46:44] "GET /static/system6.jpg HTTP/1.1" 500 59
127.0.0.1 - - [15/Oct/2021 10:46:44] "GET /favicon.ico HTTP/1.1" 404 164

post_trypyramid.py:

from pyramid.config import Configurator

if __name__ == '__main__':
    with Configurator() as config:
        config.include("pyramid_mako") 
        config.add_route('home', '/')
        config.add_route('system', '/system')
        config.add_static_view(name='static', path='static')
        config.scan('post_trypyramid_views')
        app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 6543, app)
    server.serve_forever()


post_pyramid_views.py:


from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from pyramid.response import Response
from pyramid.view import view_config

@view_config(route_name='home')
def home_view(request):
    return Response('<p>Welcome</p>')


@view_config(route_name='system', renderer='post_trypyramid_template.mako')
def form_view(request):
    return {"Nothing": "nothing"}


post_trypyramid_template.mako:

<head>
    <title>My SANDBOX</title>
    <meta charset="utf-8"/>
</head>
<body>
    <h1>Settings:</h1>
    <img src="static/system6.jpg" alt="Here you should see an image"> 
    <img src="{{ request.static_url('__main__:static/system6.jpg') }}" alt="Here you should see an image" > 
</body>
</html>

I start the application in "C:/Users/myAccount/Projects/TryPyramid" but no image is shown on the webpage, only the heading.
If I replace the "add_static_view()" call above with the absolute path like
"config.add_static_view(name='static', path='C:/Users/myAccount/Projects/TryPyramid/static')"
then at least the first "<img ...>" leads to an image in the browser.

So what is wrong with my relative path setting?? Or what else do I miss?

I have tried all sorts of modifications, like trailing or leading slashes. Nothing helped

Any hint welcome. Thanks.

vnick
  • 31
  • 2

1 Answers1

2

You are using Jinja2 syntax in a Mako template. As an aside, you should use the project name, not __main__. Try this:

<img src="${ request.static_url('myproject:static/system6.jpg') }" alt="Here you should see an image" >

If you use pyramid-cookiecutter-starter to create a starter project using Mako as the templating engine, then you can see other examples of how to build a project and proper syntax. See the documentation for a tutorial.

Steve Piercy
  • 13,693
  • 1
  • 44
  • 57