2009-01-08

Missteps in Django - Part 3

This one will make it obvious that the missteps are my own mistakes, not problems inherit in Django. I'm not terribly embarrassed to look this dumb. Most days, I'm proud of my density. On with the show!

I guess this is another template issue. Oh well. I wanted to set the CSS class on an HTML tag based on some dynamic data. In this case, the data was the status field on my Shipment model:


class Shipment(models.Model):
description = models.TextField()
created = models.DateTimeField(auto_now_add=True);
status = models.CharField(max_length=64, default="new",
choices=STATUS_CHOICES)

I'm omitting STATUS_CHOICES because its not important to my point. I wanted each displayed shipment to have a different background color. My initial idea was to try something like this:

class={% ifequal thing.status "new" %}"new"{% elifequal thing.status "exception" %}"exception"{% endelifequal %}...

Of course, Django doesn't have an elif construct in its templates. Doesn't matter because my idea was stupid and eventually I figured that I could do this:

class="status_{{ shipment.status }}"

and be done with it. I hope I had a good laugh at myself.

No comments: