在django admin list_display中有条件地更改单元格的背景颜色

时间:2016-10-27 17:33:43

标签: django list django-admin display

希望找到一种优雅的方法来有条不紊地更改list_filter中单元格的背景颜色。如果我没有条件,它适用于一种状态,但需要根据不同的状态更改背景颜色。

django版本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)

结果

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

只有一个条件

工作正常。但我相信有更好的方法可以做到这一点。

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

3 个答案:

答案 0 :(得分:2)

尝试这样的事情:

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,
    )

答案 1 :(得分:0)

我已经尝试过了,但这只是在注释文字...对我来说不是解决方案。

我终于使用Jinja在模板中解决了这个问题,在每一行中询问我要填充的值。这可能不是最优雅的解决方案,但对我有帮助。

{% 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 %}

答案 2 :(得分:0)

我用文本颜色而不是背景颜色进行了测试:

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