【问题标题】:Django Admin List_display change background color of cellDjango Admin List_display 更改单元格的背景颜色
【发布时间】:2017-03-10 08:58:13
【问题描述】:

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

【问题讨论】:

    标签: django list django-admin display


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      在旧版本中,您可以在方法中添加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
          )
      

      【讨论】:

      • 工作。现在我必须找出检查不同状态和更改颜色的最佳方法。即如果状态=关闭使颜色为绿色,否则颜色为黄色。
      猜你喜欢
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 2013-04-12
      • 2021-10-31
      • 2016-05-27
      • 1970-01-01
      相关资源
      最近更新 更多