如何从Web2PY中的View HTML加载静态文件?
-
04-10-2019 - |
题
给定布局的视图,我如何将静态文件(CSS和JS)加载到u003Chead>从视图文件?
layout.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{{=T.accepted_language or 'en'}}">
<head>
<title>{{=response.title or request.application}}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- include requires CSS files
{{response.files.append(URL(request.application,'static','base.css'))}}
{{response.files.append(URL(request.application,'static','ez-plug-min.css'))}}
-->
{{include 'web2py_ajax.html'}}
</head>
<body>
{{include}}
</body>
</html>
myview.html
{{extend 'layout.html'}}
{{response.files.append(URL(r=request,c='static',f='myview.css'))}}
<h1>Some header</h1>
<div>
some content
</div>
在上面的示例中,“ myview.css”文件被web2py忽略,要么被浏览器剥离。
那么,加载像此CSS文件的特定页面文件的最佳方法是什么?我宁愿不要将所有静态文件塞入我的布局中。
解决方案
在myview.html中反向前两行
{{response.files.append(URL(r=request,c='static',f='myview.css'))}}
{{extend 'layout.html'}}
请注意,1.78.1和1.78.2的错误不允许它起作用。它在同一天固定在1.78.3中。响应.file.append(...)也可以在需要它的控制器操作中移动。您不应该在扩展前放置逻辑,但是您将变量定义为传递到扩展视图。
不隶属于 StackOverflow