【问题标题】:why would I be getting Invalid block tag: 'static' error?为什么我会收到 Invalid block tag: 'static' 错误?
【发布时间】:2015-05-24 13:26:53
【问题描述】:

这是我的base.html的头:

{% load staticfiles %}

<head>

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />    
<!-- the CSS for Foundation - Base of client side -->
<link rel="stylesheet" href="{% static 'css/foundation.css' %}" />

<!-- the CSS for Smooth Div Scroll -->
<link rel="Stylesheet" type="text/cs' %}" href="{% static 'top_scroll/css/smoothDivScroll.css' %}" />

<!-- the CSS for Custom -->
<link rel="stylesheet" href="{% static 'css/custom.css' %} />

<script src="{% static 'js/vendor/modernizr.js' %}"></script>
</head>`

这部分是正文,上面的静态文件加载正确,我在正文中有一些我也想加载的图像,它们位于同一个文件夹中,但我已将代码写入另一个文件并使用{% include "images.html" %} 将它们添加到基础。

例如。 <img src="{% static 'top_scroll/images/demo/field.jpg' %}" alt="Demo image" id="field" /> <img src="{% static 'top_scroll/images/demo/gnome.jpg' %}" alt="Demo image" id="gnome" /> <img src="{% static 'top_scroll/images/demo/pencils.jpg' %}" alt="Demo image" id="pencils" /> <img src="{% static 'top_scroll/images/demo/golf.jpg' %}" alt="Demo image" id="golf" />

上面几乎是图像 html 文件中的所有代码,有人知道为什么我不断收到 Invalid block tag: 'static' 错误吗?我的 base.html 的顶部有 {% load staticfiles %} 所以 django 是否在每个具有静态函数的文件上都需要它,即使它被称为文本包含?

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    是的,您应该在每个使用{% static %} 标签的模板中使用{% load staticfiles %}

    摘自documentation for the {% include %} tag

    include 标签应该被认为是“渲染这个子模板并包含 HTML”的实现,而不是“解析这个子模板并包含它的内容,就好像它是父模板的一部分一样”。这意味着包含的模板之间没有共享状态——每个包含都是一个完全独立的渲染过程。

    【讨论】:

    • 非常感谢,如果我使用了 {%block%}{%endblock%} 格式,同样的事情仍然适用吗?
    • 不,{% block %} 标签是模板的一部分,因此您需要加载模板库一次。但是,如果您将 {% include %} {% block %} 标记内的某些内容,那么您必须在包含的模板内加载库。
    猜你喜欢
    • 2016-12-20
    • 2020-09-24
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2020-07-23
    • 2020-11-05
    相关资源
    最近更新 更多