为什么模板要求我在每个模板页面中导入jQuery,尽管在base.html中有一次?

时间:2018-02-06 06:55:19

标签: django django-templates

我的base.html已经包含了jQuery。我的模板扩展了基础,但不允许jquery工作,除非我在每个模板中重新插入脚本标记。发生了什么事?

base.html:

...
{% block javascript %}
      <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
{% endblock javascript %}

detail.html:

{% extends 'base.html' %}
{% load static %}

{% block content %}
...  
{% endblock content %}

{% block javascript %}  
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>  

I am required to place the script tag here again, despite having it in base.
{% endblock javascript %}

2 个答案:

答案 0 :(得分:0)

您正在使用其他模板中的javascript块,这意味着它将覆盖基本javascript块,因此您的jquery脚本应该位于 javascript块之前的base.html正文中,所以像这样

base.html

<body>
   .....
   <!-- the bottom -->
   <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
   {% block javascript %}
   {% endblock javascript %}
</body>

你可以使用{{block.super}}但是上面的修正方法是:)

答案 1 :(得分:0)

details.html

中进行
{% block javascript %}  
    {{ block.super }}
    <your jss>
{% endblock javascript %}