Stryker
Stryker

Reputation: 6120

Conditionally change background color of cell in django admin list_display

Looking to find an elegant way to conditionally change the background color of a cell in list_filter. If I don't have a conditional, it works fine for one status but need to change the color of background based on different status.

django version 1.10,

python 3.5


Model.py

class PlayBook(TimeStampModel):

minor = 'MINOR'
normal = 'NORMAL'
important = 'IMPORTANT'
critical = 'CRITICAL'

SEVERITY = (
    (minor, 'Minor'),
    (normal, 'Normal'),
    (important, 'Important'),
    (critical, 'Critical'),
)

low = 'LOW'
high = 'HIGH'
PRIORITY = (
        (low, 'Low'),
        (normal, 'Normal'),
        (high, 'High'),
        )


new = 'New'
in_progress = 'In_Progress'
needs_info = 'Needs Info'
postponed = 'Postponed'
closed = 'Closed'
STATUS= (
        (new, 'New'),
        (in_progress, 'In Progress'),
        (needs_info, 'Needs Info'),
        (postponed, 'Postponed'),
        (closed, 'Closed'),

        )

subject = models.CharField(max_length=200, unique=True)
description = models.TextField(blank=True, help_text="Business purpose of the application")
manager = models.ForeignKey(User, on_delete=models.CASCADE)

severity = models.CharField(max_length = 100, choices=SEVERITY, default=normal)
priority = models.CharField(max_length = 100, choices=PRIORITY, default=normal)
status = models.CharField(max_length = 100, choices=STATUS, default=new)
def __str__(self):
    return "{}".format(self.subject)

class Meta:
    ordering = ('severity',)
@property
def short_description(self):
    return truncatechars(self.description, 35)

Admin.py

from django.utils.html import format_html

class PlayBookAdmin(admin.ModelAdmin):
    list_display =['severity','priority', 'subject', 'status_colored','created','updated', 'short_description']


def status_colored(self, obj):
    color = 'yellow'
    if obj.status == 'Closed':
        color = 'green'
        return format_html(

            '<b style="background:{};">{}</b>',
            color,
            obj.status
                       )
    elif obj.status =='In Progress':
        color = 'yellow'
        return format_html(

            '<b style="background:{};">{}</b>',
            color,
            obj.status
                       )

    else obj.status =='Needs Info':
        color = 'orange'
        return format_html(

            '<b style="background:{};">{}</b>',
            color,
            obj.status
                       )

  status_colored.admin_order_field = 'closed'


admin.site.register(PlayBook, PlayBookAdmin)

Results

    else obj.status =='Needs Info':
           ^
SyntaxError: invalid syntax

With only one condition

Works fine. But I am sure there is a better way to do this.

from django.utils.html import format_html

class PlayBookAdmin(admin.ModelAdmin):
    list_display =['severity','priority', 'subject', 'status_colored','created','updated', 'short_description']


def status_colored(self, obj):
    color = 'yellow'
    if obj.status == 'Closed':
        color = 'green'
    return format_html(

            '<b style="background:{};">{}</b>',
            color,
            obj.status
                       )
status_colored.admin_order_field = 'closed'


admin.site.register(PlayBook, PlayBookAdmin)

enter image description here

Upvotes: 4

Views: 6305

Answers (3)

Elmar Abdulkarimov
Elmar Abdulkarimov

Reputation: 31

I made test with text color, not background color:

def visited(self, obj):
    text = 'Not Visited'
    color = 'red'
    if obj.status== True:
        text = 'Visited'
        color = 'green'
    return format_html('<b style="color:{};">{}</b>',color,text,obj.status)

this screen shot of my testing on text color

Upvotes: 3

Federico Aguirre
Federico Aguirre

Reputation: 316

I've tried this, but this just only remark the text... Were not a solutions for me.

I've finally resolve this in the template using Jinja, asking in every row for the value I want to fill. This could be not the most elegant solution, but helps me.

{% for row in cl.result_list %}
<tr class="{% cycle 'row1' 'row2' %}" {% if row.etapa == 50 %} style='background-color:#ffcccc' {% endif %} in>
    <td> {{ row.fecha }} </td>
    <td> {{ row.etapa }} </td>
    <td> {{ row.presion }} </td>
    <td> {{ row.tempin }} </td>
    <td>
</tr>{% endfor %}

Upvotes: 0

Eugene Prikazchikov
Eugene Prikazchikov

Reputation: 1904

Try something like this:

def status_colored(self, obj):
    colors = {
        'Closed': 'green',
        'In Progress': 'yellow',
        'Needs Info': 'orange',
    }
    return format_html(
        '<b style="background:{};">{}</b>',
        colors[obj.status],
        obj.status,
    )

Upvotes: 7

Related Questions