代码之家  ›  专栏  ›  技术社区  ›  koddr

Django管理员。当对象的布尔字段为=true时,如何为列表视图中的每一行添加背景色?

  •  0
  • koddr  · 技术社区  · 6 年前

    我的环境是:django 2.0.6,python 3.6.4

    我有标准的django管理(带内联编辑):。

    当对象有字段时,如何为列表视图中的每一行添加背景色 is_active_city=true ?例如, background color:green; for is_active_city=true,like this:。

    . >p standard Django Admin

    当对象有字段时,如何为列表视图中的每一行添加背景色 is_active_city=True 是吗?例如, background-color: green; 对于 活动城市=真 ,如下所示:

    1 回复  |  直到 6 年前
        1
  •  1
  •   Bijoy    6 年前

    有个包裹 django-liststyle 可以满足你的需要

    安装时使用 pip install django-liststyle==0.2b

    然后在settings.py添加 'liststyle' INSTALLED_APPS 列表。

    因此,现在相关模型的admin.py将是

    from liststyle import ListStyleAdminMixin
    
    class CityAdmin(admin.ModelAdmin, ListStyleAdminMixin):
        ...
        def get_row_css(self, obj, index):
            if obj.is_active_city:
                return 'green'
            return 'red'  # or any color for False
    

    注意 :如果存在以下问题: future 包错误,请参阅 link