如何从web2py中的视图HTML加载静态文件?

Mik*_*att 6 web2py view static-files

给定布局视图,如何从视图文件中将静态文件(基本上是CSS和JS)加载到<head>中?

的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>
Run Code Online (Sandbox Code Playgroud)

myview.html

{{extend 'layout.html'}}
{{response.files.append(URL(r=request,c='static',f='myview.css'))}}

<h1>Some header</h1>
<div>
    some content
</div>
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,"myview.css"文件被web2py忽略或被浏览器剥离.

那么加载像这个CSS文件这样的页面特定文件的最佳方法是什么?我宁愿不把所有静态文件都填充到我的布局中.

mdi*_*rro 7

在myview.html中反转前两行

{{response.files.append(URL(r=request,c='static',f='myview.css'))}}
{{extend 'layout.html'}}
Run Code Online (Sandbox Code Playgroud)

请注意1.78.1和1.78.2有一个错误不允许这个工作.它在同一天固定在1.78.3.response.file.append(...)也可以在需要它的控制器动作中移动.您不应该在扩展之前放置逻辑,而是定义要传递给扩展视图的变量.