I'm trying to make the following code DRY. The code outputs a table that shows violations. If there is any violation.status = resolved
they go in separate table for resolved items. Since the header and columns of the table are the same, I believe I should be able to make this DRY using a For loop
. However, I'm not sure that's possible in Django templates.
There's three relevant context variables:
- violation_list which is a query set of all violations
- not_resolved_violations which is (1) excluding status = resolved
- resolved_violations which is (1) filtered on status = resolved
{% if violation_list %}
{% if not_resolved_violations %}
<h3>Open Violations</h3>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center">IDENTIFIER</th>
</tr>
</thead>
<tbody>
{% for violation in not_resolved_violations %}
<tr>
<td class="text-center">{{ forloop.counter }}</td>
<td class="text-center">
<a href='#'>{{ violation.identifier }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% if resolved_violations %}
<h3>Resolved Violations</h3>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center">IDENTIFIER</th>
</tr>
</thead>
<tbody>
{% for violation in resolved_violations %}
<tr>
<td class="text-center">{{ forloop.counter }}</td>
<td class="text-center">
<a href='#'</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% else %}
<p>This project doesn't have any violations associated it with it.</p>
{% endif %}
</div>
</div>
</div>