【发布时间】:2016-12-24 06:57:13
【问题描述】:
除非我做错了,似乎如果您有嵌套模板(即,{% include %} 模板中的模板),您有时需要在嵌套的多个“层”中调用 {% load static %}。例如,假设我有templateA.html:
{% load static %}
<a href={% static "some/path" %}>Some Link</a>
{% include 'templateB.html' %}
然后在`templateB.html中,我有:
{% load static %}
<a href={% static "some/other/path" %}>Some Other Link</a>
据我测试,我必须在两个模板中都包含{% load static %},因为templateB.html 不知道我已经加载了{% static %} 标签。
我的问题是这样的:
假设需要加载{% static %} 标记两次(或更多次,具体取决于嵌套的数量),这种额外加载是否会影响性能?
我不确定当你加载这个标签时 Django 在后台做了什么,但我的直觉是你不想加载和重新加载静态文件。 (因为我们谈论的是一个开源项目,我确实尝试亲自了解一下 templatetag 是如何实现的,但事实证明它有点超出我的理解...... )。
此外,此问题假定有必要始终以这种方式加载标签。如果我缺少某些东西,我会非常有兴趣了解更多信息。谢谢!
【问题讨论】:
标签: html django django-templates django-staticfiles