Change row colour in Django Admin List

Viewed 11143

I want to highlight rows (set the backgorund colour) in the Django Admin page (change_list.html) based on a field in the model called status. What is the best way to do this?

I have 3 statuses. Open, Active, Closed. I'd like rows with open to be green, active to be orange and closed to be red.

I've found documentation about changing the templates so but not sure how to check the status to colour the row.

4 Answers
class FilmAdmin(admin.ModelAdmin):
 def get_film_status(self, obj):
        if obj.status() != 'active':
            return mark_safe("<span class='row_gray'>%s</span>" % obj.status())

 class Media:
        js = ("js/my_admin.js", )

and in my_admin.js :

window.onload = function() {
    var x=document.getElementsByClassName("row_gray");
    var i;
    for (i = 0; i < x.length; i++) {
        var ligne=x[i].parentNode;
        while (ligne.tagName !== "TR") {ligne=ligne.parentNode;}
        ligne.style.backgroundColor = "lightgray";
    }
}
Related