【问题标题】:using request.static_url() in a pyramid app在金字塔应用程序中使用 request.static_url()
【发布时间】:2012-11-28 13:43:02
【问题描述】:

在一个金字塔应用程序中,我定义了一个静态视图用于提供 css/js 文件,另一个用于提供其他数据,例如上传的文件:

config.add_static_view('static', 'static', cache_max_age=3600)
config.add_static_view('uploads', path='/data/uploads')

在模板方面,我使用 request.static_url() 来访问静态文件:

<script type="text/javascript" src="${request.static_url('myapp:static/js/jquery-1.8.3.min.js')}"></script>

问题是我不知道如何将 request.static_url() 与第二个定义(上传)一起使用,我从未工作过并导致应用程序崩溃:

${request.static_url('uploads/a_named_file')}

你能解释一下我该怎么做吗? 提前致谢。

【问题讨论】:

  • 'crash the app':在这种情况下,请始终包含完整的回溯。

标签: python pyramid static-files


【解决方案1】:

您好像忘记包含完整路径:

${request.static_url('/data/uploads/a_named_file')}

根据您为静态资产注册的匹配路径查找静态资产。对于您的两个静态视图注册,您分别使用路径static/data/uploads。第一个是相对于myapp,但第二个是绝对路径。静态视图使用绝对路径时,需要在生成静态URL时使用相同的绝对路径。

【讨论】:

  • 谢谢,它有效。使用绝对路径并不明显,因为它在 2 个不同的安装中会有所不同......这就是为什么我在 ini 文件中放置一个 upload_dir 变量并通过 request.registry.settings.get('upload_dir') 访问。
猜你喜欢
  • 1970-01-01
  • 2013-06-26
  • 2015-08-21
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 2016-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多