Pyramid 中的 add_static_view(name, 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."
不知何故,我的印象是这个描述不是 非常准确。
如果我按照以下行添加一些代码
config.add_static_view("static", "/path/to/resource/on/filesystem")
我去拜访了
http://localhost:PORT/static/logo.png
我看到给定的logo.png 它可以在
/path/to/resource/on/filesystem/
现在,如果我有如下代码
config.add_static_view("http://myfilehoster.com/images", "myproject:images")
描述“path
参数是磁盘上的路径,其中
静态文件驻留”似乎不再准确,因为实际
文件驻留在 myfilehoster 的磁盘上。
在我看来,我只是在注册某种标识符
(myproject:images),我可以在我的程序代码中使用它来引用
“真实”位置“http://myfilehoster.com/images”。
例如。
request.static_url("myproject:images/logo.png")
会解决的 到“http://myfilehoster.com/images/logo.png”。
那么这里的文档是不准确的还是我遗漏了什么?
最佳答案
你错过了什么。在narrative documentation on static assets它指出:
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 documentation使用了类似的措辞:
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 issue tracker 中提出问题。如果您对此有强烈的感觉。
关于python - Pyramid 的 add_static_view 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11689841/