Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I wanted to have alternating colours in my table using a style sheet, by passing a list of toggling True/False values. I found this really frustrating. In the end I created a list of dictionary items with the same keys as the fields in the table, plus one more with the toggling true/false value.</p> <pre><code>def jobListView(request): # django does not allow you to append stuff to the job identity, neither # will it allow forloop.counter to index another list. The only solution # is to have the toggle embedded in a dictionary along with # every field from the job j = job.objects.order_by('-priority') # have a toggling true/false list for alternating colours in the table theTog = True jobList = [] for i in j: myJob = {} myJob['id'] = i.id myJob['duty'] = i.duty myJob['updated'] = i.updated myJob['priority'] = i.priority myJob['description'] = i.description myJob['toggle'] = theTog jobList.append(myJob) theTog = not(theTog) # next i return render_to_response('index.html', locals()) # end jobDetaiView </code></pre> <p>and my template</p> <pre><code>{% if jobList %} &lt;table border="1"&gt;&lt;tr&gt; &lt;th&gt;Job ID&lt;/th&gt;&lt;th&gt;Duty&lt;/th&gt;&lt;th&gt;Updated&lt;/th&gt;&lt;th&gt;Priority&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt; &lt;/tr&gt; {% for myJob in jobList %} &lt;!-- only show jobs that are not closed and have a positive priority. --&gt; {% if myJob.priority and not myJob.closeDate %} &lt;!-- alternate colours with the classes defined in the style sheet --&gt; {% if myJob.toggle %} &lt;tr class=d1&gt; {% else %} &lt;tr class=d0&gt; {% endif %} &lt;td&gt;&lt;a href="/jobs/{{ myJob.id }}/"&gt;{{ myJob.id }}&lt;/td&gt;&lt;td&gt;{{ myJob.duty }}&lt;/td&gt; &lt;td&gt;{{ myJob.updated }}&lt;/td&gt;&lt;td&gt;{{ myJob.priority }}&lt;/td&gt; &lt;td class=middle&gt;{{ myJob.description }}&lt;/td&gt; &lt;/tr&gt; {% endif %} {% endfor %} &lt;/ul&gt; {% else %} &lt;p&gt;No jobs are in the system.&lt;/p&gt; {% endif %} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload