6

Example model:

class Article(models.Model):
    is_published = models.BooleanField(default=False)

Is there an easy way to add "is_published" css class to all in Django admin list view to all rows displaying objects with is_published==True?

3
  • docs.djangoproject.com/en/dev/ref/contrib/admin/… - Override the admin template, and add teh custom class there. Commented Jul 3, 2014 at 3:25
  • yes, that seems only way, I will try to work with that. I just need to find out how to access is_published field in template (i'm just getting whole <td>...</td> html in result) Commented Jul 4, 2014 at 17:42
  • 4
    If you're willing to use JavaScript that might be a more elegant solution. Just write a script that scans rows and changes the class based on the value in the cells. You could use class Media to add custom js & css for the ModelAdmin class. Commented Feb 23, 2016 at 22:08

1 Answer 1

3

I'm not quite sure is it what you looking for. The following example displays all "is_published" fields in red font, where "is_published==True"

class Article(models.Model):
    is_published = models.BooleanField(default=False)

    def colored_is_published(self):
        if self.is_published:
            cell_html = '<span style="color: red;">%s</span>'
        else:
            cell_html = '<span>%s</span>'
        # for below line, you may consider using 'format_html', instead of python's string formatting
        return cell_html % self.is_published   
    colored_is_published.allow_tags = True

class ArticleAdmin(admin.ModelAdmin):
    list_display = (......, 'colored_is_published')

Reference: Django list display

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but this is not enough. I need to highlight whole rows (like row1 and row2 classes)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.