テンプレートには次のようなコードがたくさんあります。
<h2>Owners of old cars</h2>
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
{% for result in old_cars %}
<tr>
<td>{{result.name}}</td>
<td>{{result.age}}</td>
<td>{{result.address}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h2>Owners of Ford cars</h2>
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
{% for result in ford_cars %}
<tr>
<td>{{result.name}}</td>
<td>{{result.age}}</td>
<td>{{result.address}}</td>
</tr>
{% endfor %}
</tbody>
</table>
上記のように、さらに多くのテーブルが作成されます。ご覧のとおり、重複したコードがたくさんあります。テーブルを作成するたびに多くのコードを繰り返さないように、これを行う方法はありますか? ありがとう。
アップデート
テーブルと呼ばれる新しいオブジェクトを作成し、結果とそれに対応する h2 タイトルを追加することを考えています。その後、テーブルと呼ばれるこれらのテーブル オブジェクトのリストを作成し、テンプレートに渡すことができます。その後、テンプレートはそれらを反復処理できます。