Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin List_display change background color of cell

Trying to change the background color if status = closed. When I try the code below the result is shown in html instead of an actual color.

##Mode.py


from django.template.defaultfilters import truncatechars  # or truncatewords

class TimeStampModel(models.Model):
    created = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name='Created')
    updated = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name='Modified on')

    class Meta:
        abstract = True


class Task(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

                                                                                                                             | 22     normal = 'NORMAL'
from .models import Task                                                                                                  | 23     important = 'IMPORTANT'
from django.contrib import admin                                                                                             | 24     critical = 'CRITICAL'
from django.contrib.auth.models import Group                                                                                 | 25
from django.contrib.auth.models import User                                                                                  | 26     SEVERITY = (
                                                                                                                             | 27         (minor, 'Minor'),
from django.http import HttpResponse                                                                                         | 28         (normal, 'Normal'),
                                                                                                                             | 29         (important, 'Important'),
                                                                                                                             | 30         (critical, 'Critical'),
class TaskAdmin(admin.ModelAdmin):                                                                                        | 31     )
    list_display =['severity','priority', 'subject', 'status_colored','created','short_description']                         | 32
                                                                                                                             | 33     low = 'LOW'
                                                                                                                             | 34     high = 'HIGH'
    def status_colored(self, obj):                                                                                           | 35     PRIORITY = (
        color = 'red'                                                                                                        | 36             (low, 'Low'),
        if obj.status != 'closed':                                                                                           | 37             (normal, 'Normal'),
            color = 'green'                                                                                                  | 38             (high, 'High'),
        return u'<b style="background:{};">{}</b>'.format(color, obj.status)                                                 | 39             )
        status_colored.allow_tags = True                                                                                     | 40
        status_colored.admin_order_field = 'closed'                                                                          | 41
                                                                                                                             | 42     new = 'New'
admin.site.register(Task, TaskAdmin)                                                                                   | 43     in_progress = 'In_Progress'

This is what I get:

Result displays html code in List_display

<b style="background:green;">Closed</b>

enter image description here

like image 659
Stryker Avatar asked Dec 14 '22 02:12

Stryker


1 Answers

In older versions, you could add an allow_tags attribute to the method to prevent auto-escaping. This attribute is deprecated since 1.9. So it’s safer to use format_html(), format_html_join(), or mark_safe()instead.

from django.utils.html import format_html

return format_html(
        '<b style="background:{};">{}</b>,
        color,
        obj.status
    )
like image 179
Sagar Ramachandrappa Avatar answered May 02 '23 02:05

Sagar Ramachandrappa