How To Use Django Cycle Tag

2024/10/8 2:30:01

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 %}
Answer

For the cycle tag, the two arguments will be added alternately to the template, for example, the code in your template {% for o in some_list %}<tr class="{% cycle 'row1' 'row2' %}"></tr>{% endfor %} would generate:

<tr class="row1">...
</tr>
<tr class="row2">...
</tr>
<tr class="row1">...
</tr>
<tr class="row2">...
</tr>
<!-- ... and so on while there are elements in some_list -->

And so on. Normally you would attach some css to this, for example:

<style>.row1 {background: #345678;}.row2 {background: #123456;}
</style>

This would give alternate rows different background colours, for example.

https://en.xdnf.cn/q/70167.html

Related Q&A

Multiple linear regression with python

I would like to calculate multiple linear regression with python. I found this code for simple linear regressionimport numpy as npfrom matplotlib.pyplot import *x = np.array([1, 2, 3, 4, 5])y = np.arra…

How to find source of error in Python Pickle on massive object

Ive taken over somebodys code for a fairly large project. Im trying to save program state, and theres one massive object which stores pretty much all the other objects. Im trying to pickle this object,…

What is the most idiomatic way to index an object with a boolean array in pandas?

I am particularly talking about Pandas version 0.11 as I am busy replacing my uses of .ix with either .loc or .iloc. I like the fact that differentiating between .loc and .iloc communicates whether I a…

Error: Command failed with rc=65536 python and mod_wsgi

im having this problem: im runing pythonbrew to get python2.7, and so i re-compiled mod_wsgi to use the 2.7 python. to that end, i followed this tutorial: code.google.com/p/modwsgi/wiki/QuickInstallati…

Why cant I freeze_panes on the xlsxwriter object pandas is creating for me?

I have a class whose objects contain pandas dataframes (self.before, and self.after below) and a save() method which uses xlsxwriter to export the data (which has two worksheets, "before" and…

Python multiprocessing pipe recv() doc unclear or did I miss anything?

I have been learning how to use the Python multiprocessing module recently, and reading the official doc. In 16.6.1.2. Exchanging objects between processes there is a simple example about using pipe t…

How would you unit test this SQLAlchemy Core query/function?

Im working on learning how to unit test properly. Given this function...def get_user_details(req_user_id):users = sa.Table(users, db.metadata, autoload=True)s = sa.select([users.c.username,users.c.favo…

Retrieve wall-time in Python using the standard library?

How can I retrieve wall-time in Python using the standard library?This question, and this question would suggest that something like clock_gettime(CLOCK_MONOTONIC_RAW) or /proc/uptime are most appropr…

NLTK: Package Errors? punkt and pickle?

Basically, I have no idea why Im getting this error. Just to have more than an image, here is a similar message in code format. As it is more recent, the answer of this thread has already been mentione…

Is there a bit-wise trick for checking the divisibility of a number by 2 or 3?

I am looking for a bit-wise test equivalent to (num%2) == 0 || (num%3) == 0.I can replace num%2 with num&1, but Im still stuck with num%3 and with the logical-or.This expression is also equivalent …