Pyramid 的 add_static_view 是如何工作的? [英] How does Pyramid's add_static_view work?
问题描述
Pyramid 中的 add_static_view(name, path) 是如何工作的?
How does add_static_view(name, path) in Pyramid work?
来自文档字符串:
"name
参数是一个表示应用程序相关的字符串本地 URL 前缀.它也可以是一个完整的 URL.path
参数是静态文件在磁盘上的路径居住.这可以是一个绝对路径,一个包相对路径,或资产规范."
"The
name
argument is a string representing an application-relative local URL prefix. It may alternately be a full URL. Thepath
argument is the path on disk where the static files reside. This can be an absolute path, a package-relative path, or an asset specification."
不知何故,我觉得这个描述不是非常准确.
Somehow I have got the impression that this description is not very accurate.
如果我添加一些代码
config.add_static_view("static", "/path/to/resource/on/filesystem")
我访问
http://localhost:PORT/static/logo.png
我看到了给定的 logo.png它可以在
I see the logo.png given that it can be found in
/path/to/resource/on/filesystem/
现在,如果我有一些像下面这样的代码
Now, if I have some code like the following
config.add_static_view("http://myfilehoster.com/images", "myproject:images")
path
参数是磁盘上的路径的描述静态文件驻留"似乎不再准确,因为实际文件驻留在 myfilehoster 的磁盘上.在我看来,我只是在注册某种标识符(myproject:images) 我可以在我的程序代码中使用以引用真实"位置http://myfilehoster.com/images".例如.
the description that "the path
argument is the path on disk where
the static files reside" does not seem accurate anymore because the actual
files reside on the disk of myfilehoster.
It seems to me that I am merely registering some kind of identifier
(myproject:images) that I can use within my program code to reference
the "real" location "http://myfilehoster.com/images".
E.g.
request.static_url("myproject:images/logo.png")
会解决到http://myfilehoster.com/images/logo.png".
would be resolved to "http://myfilehoster.com/images/logo.png".
所以这里的文档不准确还是我遗漏了什么?
So is the documentation inaccurate here or am I missing something?
推荐答案
您遗漏了一些东西.在关于静态资产的叙述性文档 它指出:
You are missing something. In the narrative documentation on static assets it states:
add_static_view()
调用的 name
参数可以代替表示 URL 前缀,也可以是一个 URL.到目前为止,我们看到的每个示例都显示了 name
参数作为 URL 前缀的用法.但是,当 name
是 URL 时,可以从外部网络服务器提供静态资产.在这种模式下,使用 pyramid.request.Request.static_url()
生成 URL 时,name
用作 URL 前缀.
Instead of representing a URL prefix, the
name
argument of a call toadd_static_view()
can alternately be a URL. Each of examples we’ve seen so far have shown usage of thename
argument as a URL prefix. However, whenname
is a URL, static assets can be served from an external webserver. In this mode, thename
is used as the URL prefix when generating a URL usingpyramid.request.Request.static_url()
.
在 API 文档中 使用了类似的措辞:
In the API documentation similar wording is used:
当使用表示 URL 前缀的 name
参数调用 add_static_view
时,如上所示,后续调用 pyramid.request.Request.static_url()
以 path
参数开头的路径传递给 add_static_view
将生成类似于 http://<Pyramid app URL>/images/logo.png
,这将导致 mypackage
包的图像子目录中的 logo.png
文件被提供.
When
add_static_view
is called with aname
argument that represents a URL prefix, as it is above, subsequent calls topyramid.request.Request.static_url()
with paths that start with thepath
argument passed toadd_static_view
will generate a URL something likehttp://<Pyramid app URL>/images/logo.png
, which will cause thelogo.png
file in the images subdirectory of themypackage
package to be served.
使用 URL 完全切换 add_static_view
的行为,并且 path
参数被解释为仅用于 .static_url()
的符号路径方法.后一个细节可能没有在文档中明确描述,您可以在 pyramid 问题跟踪器 如果你对此有强烈的感觉.
Using a URL switches the behaviour of add_static_view
altogether and the path
argument is interpreted as a symbolic path only for the .static_url()
method. That latter detail is perhaps not described as explicitly in the documentation, you could file an issue in the pyramid issue tracker if you feel strongly about that.
这篇关于Pyramid 的 add_static_view 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!