【问题标题】:Writing script tag outside or inside block?在块外部或内部编写脚本标签?
【发布时间】:2019-12-18 00:49:09
【问题描述】:

请考虑以下代码。

<!--templates/home.html-->
{% extends 'base.html' %}
{% load static %}

{% block content %}
  {% for post in object_list %}
  <div class = 'post-entry'>
    <h2><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</h2>
      <p>{{ post.body }}</p>
  </div>
  {% endfor %}
  <script type = "text/javascript" src = "{% static 'js/test.js' %}"></script>
{% endblock content %}

<!--templates/home.html-->
{% extends 'base.html' %}
{% load static %}

{% block content %}
  {% for post in object_list %}
  <div class = 'post-entry'>
    <h2><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</h2>
      <p>{{ post.body }}</p>
  </div>
  {% endfor %}
{% endblock content %}
<script type = "text/javascript" src = "{% static 'js/test.js' %}"></script>

第一个成功执行,但第二个没有。是否有必要从 django 模板块中加载外部静态文件,如果没有,那么为什么第二个代码不执行?

PS:我是 django 新手。

为了清楚起见,我还在此处提供基本模板的代码。

<!--templates/base.html-->
{% load static %}
<html>
  <head><title>Django Blog</title>
    <link href = "{% static 'css/base.css' %}" rel = "stylesheet">
  </head>
  <body>
    <header><h1><a href = "{% url 'home' %}">Django Blog</a></h1></header>
    <div>
      {% block content %}
      {% endblock content %}
    </div>
  </body>
</html>

【问题讨论】:

  • 如果你{% extends ... %} 是一个基本模板,那么{% block ... %} 之外的所有内容({% load ... %}s 等除外)都不会添加到模板中,因为它没有{% block ... %}把它放进去。

标签: python django django-templates


【解决方案1】:

第一个成功执行,但第二个没有。是否有必要从 django 模板块中加载外部静态文件,如果没有,那么为什么第二个代码不执行?

如果你覆盖一个基本模板,你只能“填充块”可以这么说。 Django 应该在哪里写你在块之外写的东西?在文件的开头?在文件的末尾?介于两者之间?

documentation on template inheritance [Django-doc]中指定:

Django 模板引擎中最强大的——因此也是最复杂的——部分是模板继承。模板继承允许您构建一个基本“骨架”模板,其中包含您网站的所有常见元素并定义子模板可以覆盖的块

但是,您可以定义 多个 块。例如,通常在末尾添加一个块,您可以在其中添加一些额外的 JavaScript,例如:

<!--templates/base.html-->
{% load static %}
<html>
  <head><title>Django Blog</title>
    <link href="{% static 'css/base.css' %}" rel="stylesheet">
  </head>
  <body>
    <header><h1><a href="{% url 'home' %}">Django Blog</a></h1></header>
    <div>
      {% block content %}
      {% endblock content %}
    </div>
  {% block js %}
  {% endblock %}
  </body>
</html>

那么你可以在页面底部写&lt;script ...&gt;部分,比如:

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

{% block content %}
  {% for post in object_list %}
  <div class='post-entry'>
    <h2><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</h2>
      <p>{{ post.body }}</p>
  </div>
  {% endfor %}
{% endblock %}

{% block js %}
<script type="text/javascript" src="{% static 'js/test.js' %}"></script>
{% endblock %}

您当然可以在{% block ...%} ... %{ endblock %} 部分之外定义变量等。但是,如果您从基本模板继承,那么在外部渲染的所有内容都会被忽略。

【讨论】:

    【解决方案2】:

    对于第二个脚本,您使用 django 命令 {% static 'js/test.js' %} 指定 script elementsrc 属性,要使其工作,它需要位于 django 块内,

    如果您想在不使用 django 块的情况下执行此操作,则需要在不使用 django 命令的情况下指定 src 属性的值,您应该像只使用 html 一样执行此操作,

    <script type = "text/javascript" src = "path_to test.js"></script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      相关资源
      最近更新 更多