gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:58:11 28 4
gpt4 key购买 nike

如果状态 = 已关闭,则尝试更改背景颜色。
当我尝试下面的代码时,结果以 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)

----

管理文件
                                                                                                                             | 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>

enter image description here

最佳答案

(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'))

关于Django Admin List_display 更改单元格的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40288605/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com