DRY for rendering same header table for different content 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 %}
&lt;h3&gt;Open Violations&lt;/h3&gt;
&lt;table class="table table-striped table-bordered table-condensed"&gt;
    &lt;thead&gt;
    &lt;tr&gt;
        &lt;th class="text-center"&gt;#&lt;/th&gt;
        &lt;th class="text-center"&gt;IDENTIFIER&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
&lt;tbody&gt;
{% for violation in not_resolved_violations %}


    &lt;tr&gt;
        &lt;td class="text-center"&gt;{{ forloop.counter }}&lt;/td&gt;
        &lt;td class="text-center"&gt;
            &lt;a href='#'&gt;{{ violation.identifier }}&lt;/a&gt;&lt;/td&gt;
     &lt;/tr&gt;
{% endfor %}
&lt;/tbody&gt;
&lt;/table&gt;

{% endif %}


{% if resolved_violations %}
&lt;h3&gt;Resolved Violations&lt;/h3&gt;
&lt;table class="table table-striped table-bordered table-condensed"&gt;
    &lt;thead&gt;
    &lt;tr&gt;
        &lt;th class="text-center"&gt;#&lt;/th&gt;
        &lt;th class="text-center"&gt;IDENTIFIER&lt;/th&gt;
                                    &lt;/tr&gt;
    &lt;/thead&gt;
&lt;tbody&gt;
{% for violation in resolved_violations %}


    &lt;tr&gt;
        &lt;td class="text-center"&gt;{{ forloop.counter }}&lt;/td&gt;
        &lt;td class="text-center"&gt;
            &lt;a href='#'&lt;/a&gt;&lt;/td&gt;

     &lt;/tr&gt;
{% endfor %}
&lt;/tbody&gt;
&lt;/table&gt;

{% endif %}


{% else %}
&lt;p&gt;This project doesn't have any violations associated it with it.&lt;/p&gt;
{% endif %}

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
You might try building a slightly more complex datastructure to be passed in.  If you built a list like:

violations = [ ("Resolved", resolved_violations), 
               ("Open", unresolved_violations) ]


Then you could write a template like:

{% for vtype, vlist in violations %}
    {% if vlist %}
&lt;h3&gt;{{ vtype }} Violations&lt;/h3&gt;
&lt;table class="table table-striped table-bordered table-condensed"&gt;
    &lt;thead&gt;
    &lt;tr&gt;
        &lt;th class="text-center"&gt;#&lt;/th&gt;
        &lt;th class="text-center"&gt;IDENTIFIER&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
&lt;tbody&gt;
        {% for violation in vlist %}
    &lt;tr&gt;
        &lt;td class="text-center"&gt;{{ forloop.counter }}&lt;/td&gt;
        &lt;td class="text-center"&gt;
            &lt;a href='#'&gt;{{ violation.identifier }}&lt;/a&gt;&lt;/td&gt;
     &lt;/tr&gt;
        {% endfor %}
&lt;/tbody&gt;
&lt;/table&gt;
    {% endif %}
{% endfor %}