1
\$\begingroup\$

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:

  1. violation_list which is a query set of all violations
  2. not_resolved_violations which is (1) excluding status = resolved
  3. 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>
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

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 %}
<h3>{{ vtype }} 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 vlist %}
    <tr>
        <td class="text-center">{{ forloop.counter }}</td>
        <td class="text-center">
            <a href='#'>{{ violation.identifier }}</a></td>
     </tr>
        {% endfor %}
</tbody>
</table>
    {% endif %}
{% endfor %}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thanks! I knew a list would fix my problem. Not sure why I kept on trying to do it in template instead of the view. I believe {% vtype %} should be replaced with {{ vtype }} in your example. Edit: Also to be able to show the no violations available message I added the following if statement: `{% if violations.0.1 or violations.1.1 %} \$\endgroup\$ Jul 15, 2017 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.