如果status =已关闭,请尝试更改背景颜色。 当我尝试下面的代码时,结果以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'
这就是我得到的:
<b style="background:green;">Closed</b>
答案 0 :(得分:6)
在旧版本中,您可以向方法添加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
)
答案 1 :(得分:2)
(A)如果您的缩进是正确的,那么def status_colored(self, obj):
return '<b style="background:{};">{}</b>'.format('red', 'Foo')
status_colored.allow_tags = True
的方法就可以了。 (即使它被弃用,它仍然可以在Django 1.10中使用。但我会建议Sagar的建议或(B)在下面。)
这是我的最低限度测试:
safe
(B)另一种方法是将字符串标记为from django.utils.safestring import mark_safe
def status_colored(self, obj):
return mark_safe('<b style="background:{};">{}</b>'.format('red', 'Foo'))
。这不是Django Admin特有的,但适用于所有Django:
{{1}}