Method 1: The Cross-Browser CSS Way

The easiest way to do this is to make use of the built-in Django {% cycle %} tag. Here’s how to use it for a table containing blog entries:

1
2
3
4
5
6
7
8
9
10
11
12
13
<table>
<tbody>
{% for blog in blogs %}
{% for entry in blog.entries %}
<tr class="{% cycle 'odd' 'even' %}">
{{entry.date}}
{{entry.title}}
{{entry.comments}}
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>

Method 2: The Pure CSS Way

1
2
tbody tr:nth-child(even) td {background: #bbeebb;}
tbody tr:nth-child(odd) td {background: #e5f9e5;}