【发布时间】:2022-01-23 16:52:46
【问题描述】:
我在 Django 中构建一个 html 页面,其结构如下:
{% extends 'main/base_template.html' %}
{% load filters %}
{% block main_window %}
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Stuff</h1>
</div>
<div class="row mb-3">
<div class="col-12 col-md-6">
{% include 'main/stuff/stuff_A.html' %}
</div>
<div class="col-12 col-md-6">
{% include 'main/stuff/stuff_B.html' %}
</div>
</div>
<div class="row mb-3">
<div class="col-12 col-md-6">
{% include 'main/stuff/stuff_C.html' %}
</div>
<div class="col-12 col-md-6">
{% include 'main/stuff/stuff_D.html' %}
</div>
</div>
{% endblock %}
并且每个stuff_X html文件的结构都是一样的,例如:
<div class="row mb-3">
<div class="table-responsive">
<table class="table table-striped table-sm caption-top" style="width:40em">
<caption>Data</caption>
<thead>
<tr>
<th scope="col">Data1</th>
<th scope="col">Data2</th>
<th scope="col">Data3</th>
</tr>
</thead>
<tbody>
<tr>
<td id="stuff_a_data1" style="width:5em">{{ stuff_a.data1 }}</td>
<td id="stuff_a_data2" style="width:5em">{{ stuff_a.data2 }}</td>
<td id="stuff_a_data3" style="width:5em">{{ stuff_a.data2 }}</td>
</tr>
</tbody>
</table>
</div>
</div>
所以真正改变的只有一些 id 和要从 django 上下文中检索的数据的名称。
是否有任何机制允许我为这种情况编写通用页面?因为目前我正在处理 4 个(几乎)相同的页面,当我需要修改某些内容时,会在每个页面中添加 sn-ps 代码。
提前致谢!
【问题讨论】:
-
很遗憾没有,因为我使用的是 django 默认模板系统,而不是 Jinja
标签: python django django-templates