简体   繁体   English

如何将html文件中存储的许多片段收集到Django中的一个模板中?

[英]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.

相关问题 如何在Django中以多对一关系将一个模型对象添加到另一模型的ModelForm模板中? - How do I add one model objects to another model’s ModelForm template with many to one relationship in Django? Django中如何将一对多关系的多个值解析为HTML模板? - How to parse the many values of one-to-many relationship into HTML template in Django? 如何在 django 中实现一对多关系? - How do I implement one to many relationship in django? 我如何将单个对象从 django 导入到 html 模板 - How do i import single object from django to a html template 如何在Django模板中发出外部网站的HTML? - How do I emit the HTML of an external website in a Django template? 如何通过一对多关系查询 django 父子关系? - How do I query django parent and child by a one to many relationship? 如何在我的 django HTML 模板中显示多对多字段? - How to show a Many-to-Many field in my django HTML Template? 如何访问Django模板列表中实体之一的属性? - How do I access the property of one of the entities in a list in a Django template? 如何将两个视图链接到一个模板文件 Django - How do I link two views to one template file in Django 如何从 Django 模板访问多对多“直通”表的属性? - How do I access the properties of a many-to-many "through" table from a django template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM