URL派发如何使用金字塔服务于提供静态图像文件?

问题描述

我从快速教程页面here创建了一个简单的Pyramid应用程序,其中包含与问题相关的以下文件

  1. private void DgvGrd_SizeChanged(object sender,EventArgs e) { dgvGrd.Columns[0].Width = (int)(dgvGrd.Width * 0.2); dgvGrd.Columns[1].Width = (int)(dgvGrd.Width * 0.2); dgvGrd.Columns[2].Width = (int)(dgvGrd.Width * 0.4); dgvGrd.Columns[3].Width = (int)(dgvGrd.Width * 0.2); // also may be a good idea to set FILL for the last column // to accomodate the round up in conversions dgvGrd.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; }
tutorial/__init__.py
  1. 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
  1. 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> 上。现在,这是我尝试过的3种情况以及使用tutorial/static/images/test.jpeg启动服务器时的结果:

  1. pserve development.ini --reload中的路由配置:tutorial/__init__.py。当我访问config.add_route('image','/{filename}')时,可以看到该图像,并且一切正常。
  2. localhost:6543/test.jpeg中的路由配置:tutorial/__init__.py。当我访问config.add_route('image','/foo/{filename}')时,可以看到该图像,并且一切正常。
  3. localhost:6543/foo/test.jpeg中的路由配置:tutorial/__init__.py。当我访问config.add_route('image','/foo/bar/{filename}')时,这是我 看到图像的时候。

在上述情况3)中,我尝试了一些操作,并且只有在文件localhost:6543/foo/bar/test.jpeg中将行tutorial/templates/image.pt更改为<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">

另一种选择是在路由表的末尾创建一个"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')

如果需要的话,也可以配置多个静态路由。

请参见full documentation of Static Assets