python - Pyramid 的 add_static_view 是如何工作的?

标签 python web-applications pyramid

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. The path 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 to add_static_view() can alternately be a URL. Each of examples we’ve seen so far have shown usage of the name argument as a URL prefix. However, when name is a URL, static assets can be served from an external webserver. In this mode, the name is used as the URL prefix when generating a URL using pyramid.request.Request.static_url().

API documentation使用了类似的措辞:

When add_static_view is called with a name argument that represents a URL prefix, as it is above, subsequent calls to pyramid.request.Request.static_url() with paths that start with the path argument passed to add_static_view will generate a URL something like http://<Pyramid app URL>/images/logo.png, which will cause the logo.png file in the images subdirectory of the mypackage 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/

相关文章:

php - 在/var/www/html 中编辑

java - 如何在 Web 应用程序容器上为特定用户创建文件夹?

python - 存储元素的 Pyramid 授权

python - 为什么设置一个元素值会更改 Python 二维矩阵中其他元素的值

java - 数据库连接池[Hikari] 初始化错误

python - Perl 代码序列化和反序列化变量,如 python pickle

python - 我可以在 Pyramid 中拥有多个 ini 配置文件吗?

python - Pyramid 渲染的 JSON 响应以字符串形式返回

java - jmx-term python 子进程毒害 shell

python - 对 'home' 进行反向操作,未找到参数 '()' 和关键字参数 '{}'。 0 个图案