静态文件

在pylons中,应用程序的“public”目录被配置为“/”上的静态覆盖,因此url“/images/logo.png”转到“pylonsapp/public/images/logo.png”。这是使用中间件完成的。 Pyramid 没有一个完全等效的,但它有一种方法来服务静态文件,附加包提供了额外的方法。

静态视图

这是Pyramid提供静态文件的默认方式。如前一章中的主要功能所述:

config.add_static_view('static', 'static', cache_max_age=3600)

这会告诉Pyramid在url“/static”下发布目录“pyramidapp/static”,以便url“/static/images/logo.png”转到“pyramidapp/static/images/logo.png”。

It's implemented using 遍历, which we haven't talked about much in this Guide. Traversal-based views have a view name which serves as a URL prefix or component. The first argument is the view name ("static"), which implies it matches URL "/static". The second argument is the asset spec for the directory (relative to the application's Python package). The keyword arg is an option which sets the HTTP expire headers to 3600 seconds (1 hour) in the future. There are other keyword args for permissions and such.

Pyramid 的静态视图比 Pylons 具有以下优势:

  • 它鼓励所有静态文件使用一个URL前缀,这样它们就不会分散在URL空间中。

  • Methods to generate URLs are provided: request.static_url()request.static_path() .

  • 部署配置(ini文件)可以重写基本URL(“/static”),以服务来自单独静态媒体服务器(“http://static.example.com/”)的文件。

  • 部署配置还可以覆盖静态目录中的项目,改为指向其他子目录或文件。这在Pyramid手册中被称为“覆盖资产”。

与 Pylons 相比,它有以下缺点:

  • Static URLs have the prefix "/static".

  • 它不能提供顶级文件URL,如“/robots.txt”和“/favicon.ico”。

您可以为任何URL目录提供静态视图,因此您可以为每个URL目录提供单独的视图,如下所示:

config.add_static_view('images', 'static/images')
config.add_static_view('stylesheets', 'static/stylesheets')
config.add_static_view('javascript', 'static/javascript')

这将配置指向目录“pyramidapp/static/images”的url“/images”等。

如果您使用的是Pyramid授权系统,您还可以为需要特定权限的文件创建单独的视图:

config.add_static_view("private", "private", permission="admin")

Generating static URLs

You can generate a URL to a static file like this:

href="${request.static_url('static/images/logo.png')}

Top-level file URLs

那么,如何解决顶级文件URL的问题呢?您可以为它们注册普通视图,如下所示。对于“/favicon.ico”,可以将其替换为网站模板中的HTTP头:

<link rel="shortcut icon" href="${request.static_url('pyramidapp:static/favicon.ico')}" />

实际上,标准的Pyramid脚手架就是这样做的。对于“/robots.txt”,您可以决定它实际上属于Web服务器而不是应用程序,因此您可以让Apache直接这样服务它:

Alias   /robots.txt   /var/www/static/norobots.txt

当然,也可以让Apache为静态目录服务:

Alias   /static   /PATH-TO/PyramidApp/pyramidapp/static

但是如果你使用mod_proxy,你必须禁用该目录的代理。 early 在虚拟主机配置中:

Alias ProxyPass   /static   !

如果将rewriterule与其他路径指令(如alias)结合使用,请阅读rewriterule标志文档(尤其是“pt”和“f”),以确保指令按预期进行合作。

External static media server

要使静态媒体服务器的配置更灵活,请执行以下操作:

# In INI file
static_assets = "static"
# -OR-
static_assets = "http://staticserver.com/"

主要功能:

config.add_static_view(settings["static_assets"], "zzz:static")

现在,它将根据配置生成“http://mysite.com/static/foo.jpg”或“http://staticserver.com/foo.jpg”。

静态路由

This strategy is available in Akhet. It overlays the static directory on top of "/" like Pylons does, so you don't have to change your URLs or worry about top-level file URLs. ::

1config.include('akhet')
2# Put your regular routes here.
3config.add_static_route('zzz', 'static', cache_max_age=3600)
4# Arg 1 is the Python package containing the static files.
5# Arg 2 is the subdirectory in the package containing the files.

这个注册了一个匹配所有URL的静态路由,以及一个为其服务的视图。实际上,路由将有一个谓词来检查文件是否存在,如果不存在,则路由将与URL不匹配。不过,最好在其他路由之后注册静态路由。

如果前面有另一个catchall路由,可能与某些静态URL匹配,则必须从路由中排除这些URL,如本例所示:

config.add_route("main", "/{action}",
    path_info=r"/(?!favicon\.ico|robots\.txt|w3c)")
config.add_static_route('zzz', 'static', cache_max_age=3600)

静态路由实现可以 not 生成指向静态文件的URL,因此您必须自己执行。 Pylons 也从未有效地做到这一点。

Other ways to serve top-level file URLs

如果您使用的是静态视图,并且仍然需要为顶级文件URL提供服务,那么有几种方法可以做到这一点。

A manual file view

This is documented in the Pyramid manual in the Static Assets chapter. ::

 1# Main function.
 2config.add_route("favicon", "/favicon.ico")
 3
 4# Views module.
 5import os
 6from pyramid.response import FileResponse
 7
 8@view_config(route_name="favicon")
 9def favicon_view(request):
10    here = os.path.dirname(__file__)
11    icon = os.path.join(here, "static", "favicon.ico")
12    return FileResponse(icon, request=request)

或者,如果您真的很好奇如何在没有路由的情况下为遍历配置视图:

@view_config(name="favicon.ico")

pyramid_assetviews

pyramid_assetviews " is a third-party package for top-level file URLs. ::

1# In main function.
2config.include("pyramid_assetviews")
3config.add_asset_views("static", "robots.txt")  # Defines /robots.txt .
4
5# Or to register multiple files at once.
6filenames = ["robots.txt", "humans.txt", "favicon.ico"]
7config.add_asset_views("static", filenames=filenames, http_cache=3600)

当然,如果您在静态目录中有这些文件,它们仍然可以显示为“/static/robots.txt”和“/robots.txt”。如果这让您感到困扰,请在静态目录之外为它们创建另一个目录。