This hopefully is a pretty easy question. My endgoal is to be able to view my database entries in a table which I can sort via the column headers. I have read the documentation on cycle tags, but don't know they mean by 'row1'
and 'row2'
:
{% for o in some_list %}
<tr class="{% cycle 'row1' 'row2' %}">
...
</tr>
{% endfor %}
Secondly, how would I correctly implement it into my template? I am using a very simple JS library, which will allow the sorting:
page.html
{% if Variable %}
<table class="sortable">
<tr>
<th>Title 1</th>
<th>Title 2</th>
<th>Title 3</th>
<th>Title 4</th>
<th>Title 5</th>
</tr>
{% for stuff in Variable %}
<tr class="{% cycle 'stuff' %}">
<td>
<a href="{% url 'detail_page' stuff.id %}">
{{ stuff.Name|capfirst }}</a>
</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No Results Found</p>
{% endif %}
my models.py in case you need it:
def view(request):
Variable = database.objects.all()
context = {
'Variable': Variable,
}
return render(request, 'app/page.html', context)
EDIT: It seems I had the right code all along, just some unevenly applied CSS that made my table not look like a table. The cycle tag was not needed, only the for loop. It also looked better after adding another table row:
{% for stuff in Variable %}
<tr>
<td>{{ stuff.Name|capfirst }}</td>
<td>{{ stuff.Number|capfirst }}</td>
</tr>
{% endfor %}
cycle
tag here. All it does is switch back and forth between a number of values, as the forloop iterates. The "rows" in the docs is just alternating between two classnames, each time the forloop runs (i.e. on first iteration, addrow1
as a className, on the second userow2
, on the third userow1
, on the fourthrow2
etc.) – Slaterclass="row1"
andclass="row2"
back and forth while there are items in the list. – Slater