【发布时间】:2015-10-07 13:25:32
【问题描述】:
问题:
我想将多个模板包含到另一个模板中,该模板最初会被渲染。
我的方法:
渲染 index.html,它扩展 base.html 并包含其他模板(include_body.html、include_head.html)
base.html:
<html>
<head>
{% block head %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
index.html:
{% extends 'base.html' %}
{% block head %}{% include 'include_head.html' %}{% endblock %}
{% block body %}{% include 'include_body.html' %}{% endblock %}
include_head.html(将内容添加到 HTML 头元素):
<script src="./static/js/map.js"></script>
include_body.html(将内容添加到 HTML body 元素):
<p>Hallo World!</p>
views.py(根据要求渲染 index.html):
# ...
def index(request):
return render(request, 'index.html')
问题:
我的方法有效,但我对此没有信心。将头部和身体部分特别分开感觉是错误的。有更好的方法吗?如何避免将 include_head.html 与 include_body.html 一起使用,而是使用单个 include.html?
【问题讨论】: