服务Static Assets

这个配方集合描述了如何以各种方式为Static Assets提供服务。

Serving File Content Dynamically

通常,您将使用静态视图(通过“config.add_static_view”)来服务文件系统上的文件内容。但有时需要从非静态区域组合和读取文件,或者动态组合视图代码并提供(例如,可调用视图可能构造并返回PDF文件或图像)。

举例来说,这里有一个 Pyramid 应用程序,当URL /test.jpg 执行:

 1from pyramid.view import view_config
 2from pyramid.config import Configurator
 3from pyramid.response import FileResponse
 4from paste.httpserver import serve
 5
 6@view_config(route_name='jpg')
 7def test_page(request):
 8    response = FileResponse(
 9        '/home/chrism/groundhog1.jpg',
10        request=request,
11        content_type='image/jpeg'
12        )
13    return response
14
15if __name__ == '__main__':
16    config = Configurator()
17    config.add_route('jpg', '/test.jpg')
18    config.scan('__main__')
19    serve(config.make_wsgi_app())

基本上,使用 pyramid.response.FileResponse 作为响应对象并返回它。请注意 requestcontent_type 参数是可选的。如果 request 未提供,任何 wsgi.file_wrapper optimization supplied by your WSGI server will not be used when serving the file. 如果 content_type 未提供,将使用 mimetypes application/octet-stream 将使用内容类型。

从根目录为单个文件提供服务

如果需要提供单个文件,例如 /robots.txt/favicon.ico 那个 must be served from the root, you cannot use a static view to do it, as static views cannot serve files from the root (a static view must have a nonempty prefix such as /static )要解决这一限制,请“手动”创建视图,以提供原始文件数据。下面是创建两个视图的示例:一个用于 /favicon.ico 另一个发球 /robots.txt .

在启动时,两个文件都使用纯Python从磁盘上的文件读取到内存中。为每个对象创建一个响应对象。此响应由一个视图提供,该视图挂接静态文件的URL。

 1# this module = myapp.views
 2
 3import os
 4
 5from pyramid.response import Response
 6from pyramid.view import view_config
 7
 8# _here = /app/location/myapp
 9
10_here = os.path.dirname(__file__)
11
12# _icon = /app/location/myapp/static/favicon.ico
13
14_icon = open(os.path.join(
15             _here, 'static', 'favicon.ico')).read()
16_fi_response = Response(content_type='image/x-icon',
17                        body=_icon)
18
19# _robots = /app/location/myapp/static/robots.txt
20
21_robots = open(os.path.join(
22               _here, 'static', 'robots.txt')).read()
23_robots_response = Response(content_type='text/plain',
24                            body=_robots)
25
26@view_config(name='favicon.ico')
27def favicon_view(context, request):
28    return _fi_response
29
30@view_config(name='robots.txt')
31def robotstxt_view(context, request):
32    return _robots_response

根相对自定义静态视图(仅限URL调度)

这个 pyramid.static.static_view helper类生成一个可调用的 Pyramid 视图。此视图可调用可以为目录中的Static Assets提供服务。该类的一个实例实际由 pyramid.config.Configurator.add_static_view() 配置方法,因此一旦配置它,它的行为几乎完全相同。

警告

以下示例 will not work add_view (at least those without a route_name )一 pyramid.static.static_view cannot be made root-relative when you use traversal.

要在文件系统上的目录中提供文件,请执行以下操作: /path/to/static/dir as the result of a "catchall" route hanging from the root that exists at the end of your routing table, create an instance of the pyramid.static.static_view 内部A类 static.py 应用程序根目录中的文件如下:

from pyramid.static import static_view
www = static_view('/path/to/static/dir', use_subpath=True)

备注

为了获得更好的跨系统灵活性,请使用资产规范作为 pyramid.static.static_view 而不是物理绝对文件系统路径,例如 mypackage:static 而不是 /path/to/mypackage/static .

随后,您可以将此视图提供服务的文件连接为 /<filename> 在应用程序的启动代码中使用配置方法:

# .. every other add_route and/or add_handler declaration should come
# before this one, as it will, by default, catch all requests

config.add_route('catchall_static', '/*subpath', 'myapp.static.www')

The special name *subpath above is used by the pyramid.static.static_view 可调用视图,表示文件相对于所服务目录的路径。