[英]How do I collect many pieces stored in html files into one template in Django?
Supposing I have a page to display that consists of several pieces like this: 假设我要显示一个页面,该页面由以下几部分组成:
<!-- result page -->
<div>
<b>1st piece</b>
</div>
<div>
<b>2nd piece</b>
</div>
<div>
<b>3rd piece</b>
</div>
I want to keep each piece in a separate html-file: 我想将每个片段保存在单独的html文件中:
<!-- piece1.html -->
<b>1st piece</b>
<!-- piece2.html -->
<b>2nd piece</b>
<!-- piece3.html -->
<b>3rd piece</b>
And add them into a base template using Django templates: 并使用Django模板将它们添加到基本模板中:
<!-- base.html -->
<div>
{% block piece1 %}... import piece1.html ...{% endblock %}
</div>
<div>
{% block piece2 %}... import piece2.html ...{% endblock %}
</div>
<div>
{% block piece3 %}... import piece3.html ...{% endblock %}
</div>
How do I manage base.html
to make it import all pieces by their names in Django? 如何管理base.html
,使其在Django中按名称导入所有片段? Or maybe there's a different approach to do it easily? 或者,也许有另一种方法可以轻松地做到这一点?
In my project I'm going to use a complicated structure of pieces and big size for each one. 在我的项目中,我将使用一个复杂的块结构和每个块的大尺寸。 This is why I want to keep each piece in a separate html-file. 这就是为什么我想将每个片段保存在单独的html文件中。
Use the include
tag. 使用include
标记。
{% include 'piece1.html' %}
{% include 'piece2.html' %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.