URL 如何使用 Pyramid 调度工作来提供静态图像文件?
How does URL dispatch work to serve static image files using Pyramid?
我从快速教程页面 here 创建了一个简单的金字塔应用程序,其中包含与问题相关的以下文件:
tutorial/__init__.py
:
from pyramid.config import Configurator
def main(global_config, **settings):
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.add_route('home', '/')
config.add_route('hello', '/howdy')
config.add_static_view(name='static', path='tutorial:static')
config.add_route('image', '/{filename}')
config.scan('.views')
return config.make_wsgi_app()
tutorial/views/views.py
:
from pyramid.view import (
view_config,
view_defaults
)
@view_defaults(renderer='../templates/home.pt')
class TutorialViews:
def __init__(self, request):
self.request = request
@view_config(route_name='home')
def home(self):
return {'name': 'Home View'}
@view_config(route_name='hello')
def hello(self):
return {'name': 'Hello View'}
@view_config(route_name='image', renderer='../templates/image.pt')
def image(request):
filename = request.matchdict.get('filename')
return {'name': 'Hello View', 'filename': filename}
tutorial/templates/image.pt
:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Quick Tutorial: ${name}</title>
<link rel="stylesheet"
href="${request.static_url('tutorial:static/app.css') }"/>
</head>
<body>
<h1>Hi ${name}</h1>
<img src="../static/images/${filename}">
</body>
</html>
我已经在路径 tutorial/static/images/test.jpeg
中放置了一个图像文件。现在,这是我尝试的 3 种情况以及我使用 pserve development.ini --reload
:
启动服务器时的结果
tutorial/__init__.py
中的路由配置:config.add_route('image', '/{filename}')
。当我访问 localhost:6543/test.jpeg
时,我可以看到图像并且一切正常。
tutorial/__init__.py
中的路由配置:config.add_route('image', '/foo/{filename}')
。当我访问 localhost:6543/foo/test.jpeg
时,我可以看到图像并且一切正常。
tutorial/__init__.py
中的路由配置:config.add_route('image', '/foo/bar/{filename}')
。当我访问localhost:6543/foo/bar/test.jpeg
时,这是我不看到图像的时候。
在上述情况 3) 中,我尝试了一些操作,只有在文件 tutorial/templates/image.pt
中才能看到图像,我将行 <img src="../static/images/${filename}">
更改为 <img src="../../static/images/${filename}">
。我似乎无法理解为什么 Pyramid 强迫我在模板中添加另一个目录层以查看图像。谁能解释一下为什么?
我不太清楚为什么要在这个最小示例中将静态资产放在三个不同的位置。
根据您在示例 3 中提供的配置,您可以按如下方式修改您的模板。
<img src="${request.static_path("tutorial:static/images/")}${filename}">
然后应该生成 HTML 到:
<img src="http://example.com/static/images/test.jpeg">
另一种选择是在路由 table 的末尾创建一个 "catch all" route,这将直接为静态资产提供服务,而无需使用模板。
from pyramid.static import static_view
static_view = static_view('/path/to/static/dir', use_subpath=True)
# .. every other add_route declaration should come
# before this one, as it will, by default, catch all requests
config.add_route('catchall_static', '/*subpath')
config.add_view('myapp.static.static_view', route_name='catchall_static')
配置多个静态路由也是可以的,如果需要的话。
我从快速教程页面 here 创建了一个简单的金字塔应用程序,其中包含与问题相关的以下文件:
tutorial/__init__.py
:
from pyramid.config import Configurator
def main(global_config, **settings):
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.add_route('home', '/')
config.add_route('hello', '/howdy')
config.add_static_view(name='static', path='tutorial:static')
config.add_route('image', '/{filename}')
config.scan('.views')
return config.make_wsgi_app()
tutorial/views/views.py
:
from pyramid.view import (
view_config,
view_defaults
)
@view_defaults(renderer='../templates/home.pt')
class TutorialViews:
def __init__(self, request):
self.request = request
@view_config(route_name='home')
def home(self):
return {'name': 'Home View'}
@view_config(route_name='hello')
def hello(self):
return {'name': 'Hello View'}
@view_config(route_name='image', renderer='../templates/image.pt')
def image(request):
filename = request.matchdict.get('filename')
return {'name': 'Hello View', 'filename': filename}
tutorial/templates/image.pt
:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Quick Tutorial: ${name}</title>
<link rel="stylesheet"
href="${request.static_url('tutorial:static/app.css') }"/>
</head>
<body>
<h1>Hi ${name}</h1>
<img src="../static/images/${filename}">
</body>
</html>
我已经在路径 tutorial/static/images/test.jpeg
中放置了一个图像文件。现在,这是我尝试的 3 种情况以及我使用 pserve development.ini --reload
:
tutorial/__init__.py
中的路由配置:config.add_route('image', '/{filename}')
。当我访问localhost:6543/test.jpeg
时,我可以看到图像并且一切正常。tutorial/__init__.py
中的路由配置:config.add_route('image', '/foo/{filename}')
。当我访问localhost:6543/foo/test.jpeg
时,我可以看到图像并且一切正常。tutorial/__init__.py
中的路由配置:config.add_route('image', '/foo/bar/{filename}')
。当我访问localhost:6543/foo/bar/test.jpeg
时,这是我不看到图像的时候。
在上述情况 3) 中,我尝试了一些操作,只有在文件 tutorial/templates/image.pt
中才能看到图像,我将行 <img src="../static/images/${filename}">
更改为 <img src="../../static/images/${filename}">
。我似乎无法理解为什么 Pyramid 强迫我在模板中添加另一个目录层以查看图像。谁能解释一下为什么?
我不太清楚为什么要在这个最小示例中将静态资产放在三个不同的位置。
根据您在示例 3 中提供的配置,您可以按如下方式修改您的模板。
<img src="${request.static_path("tutorial:static/images/")}${filename}">
然后应该生成 HTML 到:
<img src="http://example.com/static/images/test.jpeg">
另一种选择是在路由 table 的末尾创建一个 "catch all" route,这将直接为静态资产提供服务,而无需使用模板。
from pyramid.static import static_view
static_view = static_view('/path/to/static/dir', use_subpath=True)
# .. every other add_route declaration should come
# before this one, as it will, by default, catch all requests
config.add_route('catchall_static', '/*subpath')
config.add_view('myapp.static.static_view', route_name='catchall_static')
配置多个静态路由也是可以的,如果需要的话。