Django Admin List_display 更改单元格的背景颜色

Django Admin List_display change background color of cell

如果状态 = 关闭,则尝试更改背景颜色。 当我尝试下面的代码时,结果显示在 html 而不是实际颜色中。

##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'

这是我得到的:

结果在List_display

中显示html代码
<b style="background:green;">Closed</b>

在旧版本中,您可以向该方法添加一个 allow_tags 属性以防止自动转义。此属性自 1.9 起已弃用。所以使用 format_html()format_html_join()mark_safe() 更安全。

from django.utils.html import format_html

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

(A) 如果您的缩进是正确的,您使用 allow_tags 的方法将会奏效。 (即使它已被弃用,它甚至在 Django 1.10 中仍然有效。但我会推荐 Sagar 的建议或下面的 (B)。)

这是我的最小测试:

def status_colored(self, obj):
    return '<b style="background:{};">{}</b>'.format('red', 'Foo')

status_colored.allow_tags = True

(B) 另一种方法是将字符串标记为 safe。这不是特定于 Django Admin 的,但适用于所有 Django:

from django.utils.safestring import mark_safe

def status_colored(self, obj):
    return mark_safe('<b style="background:{};">{}</b>'.format('red', 'Foo'))