[Fixed]-Django Template – neat 1-1 translation of values (dictionary lookup)

1👍

Add a helper function to the object, so that you don’t need any logic in the template?

Add to StashURLJobRequest

def status_display_colour(self): 
  return {
    "Done":"status_green", 
    "Requested":"status_amber", 
    "Failed":"status_red"
         }[ self.status_display() ]

And the template becomes <div class="width5pc textcentre sans texttiny {{ job_instance.status_display_colour }} ...

Another way would be class="... my-class-{{job_instance.status_display}}" with my-class-Done, my-class-Requested, my-class-Failed appropriately defined in your css. It’s inferior because it depends implicitly on status_display remaining a single word with no spaces or other special characters.

Leave a comment