【问题标题】:Pyramid can not access static asset (neither an .jpg image nor a .js file)Pyramid 无法访问静态资产(既不是 .jpg 图像也不是 .js 文件)
【发布时间】:2021-12-03 12:19:40
【问题描述】:

我有一个更大的测试用例,它使用 Pyramid 和 Mako 以及 Javascript。除非我将 Javascript 代码放入单独的文件中,否则它工作正常。然后它失败了。 我已将测试用例缩减为以下(3 个文件),用我想加载的简单图片替换 JS 文件,但同样的错误消息也失败了:

NotImplementedError:无法对未注册的加载程序类型执行此操作
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>

我在“C:/Users/myAccount/Projects/TryPyramid”中启动应用程序,但网页上没有显示图像,只有标题。
如果我将上面的“add_static_view()”调用替换为绝对路径,例如
“config.add_static_view(name='static', path='C:/Users/myAccount/Projects/TryPyramid/static')”
那么至少第一个“”会导致浏览器中的图像。

那么我的相对路径设置有什么问题?或者我还想念什么?

我尝试了各种修改,例如尾随或前导斜杠。没有任何帮助

欢迎任何提示。谢谢。

【问题讨论】:

    标签: javascript static assets pyramid mako


    【解决方案1】:

    您在 Mako 模板中使用 Jinja2 语法。顺便说一句,您应该使用项目名称,而不是 __main__。试试这个:

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

    如果您使用pyramid-cookiecutter-starter 创建一个使用 Mako 作为模板引擎的入门项目,那么您可以看到其他有关如何构建项目和正确语法的示例。请参阅documentation for a tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 2023-03-05
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多