【发布时间】: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