小编das*_*gon的帖子

为什么要为每个模板加载静态文件,即使它已被扩展?

我有一个base.html文件,其中包含一些"随机"HTML代码,我有以下代码:

{% load staticfiles %}
<!DOCTYPE html>
<html>
   <head>
      ... 
     {% block extra_js_top %}{% endblock %}
   </head>
   ...
</html>
Run Code Online (Sandbox Code Playgroud)

在我的index.html文件中,我扩展了base.html并加载了一些额外的javascript文件:

{% extends "base.html" %}
...
{% block extra_js_top %}
   <script type="text/javascript" src="{% static "js/somejs.js" %}"></script>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

问题是因为静态var而无法加载额外的javascript.即使我扩展了具有模板内部的base.html,它也不会加载{% load staticfiles %}.最后我解决了{% load staticfiles %}index.html添加一个问题的问题.

我的问题是为什么我们应该添加{% load staticfiles %}我们使用的每个模板,即使我们已经扩展了一个已经存在的文件?

python django static django-templates

22
推荐指数
1
解决办法
5530
查看次数

标签 统计

django ×1

django-templates ×1

python ×1

static ×1