更改 Django 管理列表中的行颜色

发布于 2024-09-28 22:06:52 字数 238 浏览 0 评论 0原文

我想根据模型中名为 status 的字段突出显示 Django 管理页面 (change_list.html) 中的行(设置背景颜色)。最好的方法是什么?

我有3个状态。开放式、活动式、封闭式。我希望打开的行为绿色,活动的行为橙色,关闭的行为红色。

我找到了有关更改模板的文档,但不确定如何检查状态以对行进行着色。

I want to highlight rows (set the backgorund colour) in the Django Admin page (change_list.html) based on a field in the model called status. What is the best way to do this?

I have 3 statuses. Open, Active, Closed. I'd like rows with open to be green, active to be orange and closed to be red.

I've found documentation about changing the templates so but not sure how to check the status to colour the row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野鹿林 2024-10-05 22:06:52

我错了,还有另一种选择,仅使用 Django 管理应用程序。

在应用程序的 admin.py 中,您可以为表格单元格的内容定义渲染器。这是我的电影库的变体:

class FilmAdmin(admin.ModelAdmin):

    def film_status(self, obj):
        if obj.status() != 'active':
            return '<div style="width:100%%; height:100%%; background-color:orange;">%s</div>' % obj.status()
        return obj.status()
    film_status.allow_tags = True

    list_display = ('id', 'title', 'film_status')

admin.site.register(Film, FilmAdmin)

在这里,我创建了一个 Film 模型中不存在的字段名称 film_status,并将其定义为 FilmAdmin 的方法>。它会传递每一行的项目。我必须告诉渲染器 allow_tags,它告诉管理应用程序不要“保护”HTML 内容。

但这不会填充整个单元格,因为单元格本身有一些填充。只有您的应用程序允许填充的单元格部分(由管理员的样式表定义)才会被填充。但这对于我的目的来说已经足够了。

就这样吧。两种完全不同但有用的技术用于装饰 Django 管理列表中单元格的内容。

I was wrong, there's another alternative using just the Django admin app.

In the admin.py for your app, you can define a renderer for the contents of a table cell. Here's the variant for my film library:

class FilmAdmin(admin.ModelAdmin):

    def film_status(self, obj):
        if obj.status() != 'active':
            return '<div style="width:100%%; height:100%%; background-color:orange;">%s</div>' % obj.status()
        return obj.status()
    film_status.allow_tags = True

    list_display = ('id', 'title', 'film_status')

admin.site.register(Film, FilmAdmin)

Here, I've created a field name, film_status, that does not exist in the Film model, and defined it as a method of FilmAdmin. It gets passed the item for every row. I've had to tell the renderer to allow_tags, which tells the admin app not to "safe" the HTML content.

This won't fill the whole cell, though, as the cell itself has some padding. Only the part of the cell your app is allowed to fill (as defined by the admin's stylesheet) will be filled. But it's good enough for my purposes.

There you go. Two completely different, but useful, techniques for decorating the content of a cell in a Django admin list.

高冷爸爸 2024-10-05 22:06:52

查看django-liststyle,这正是您所追求的。

Check out django-liststyle, exactly what you're after.

離人涙 2024-10-05 22:06:52

事实证明,自定义change_list_results.html 和关联的生成器是相当困难的。我想提出一个完全不同的解决方案:为您的应用程序覆盖change_list.html,并使用Javascript来实现您想要的效果。

我遇到了与你完全相同的问题。对于电影库,我需要知道电影制片人的注册是否“有效”或其他。这是我的覆盖的全部内容:

{% extends "admin/change_list.html" %}

{% block extrahead %}
{{ block.super }}
<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        $('#result_list tr td:nth-child(7)').each(function() {
            if ($(this).text() != 'active') {
                $(this).css('background-color', 'orange');
            }
        });
    });
})(django.jQuery);
</script>
{% endblock %}

此文件是 ${TEMPLATE_DIR}/admin/films/film/change_list.html。 Django 的结果列表是 id'd result_list 的,我在这里所做的只是用不同的背景样式装饰第 7 列,如果该列的内容不符合我的喜好。

管理员已经提供了 jQuery,因此无需对应用程序或管理员进行任何其他更改即可实现此功能。

As it turns out, customizing the change_list_results.html and the associated generator is quite a bear. I'd like to propose a completely different solution: Override change_list.html for your application, and use Javascript to do the effect you want.

I had exactly the same problem you have. For a film library, I needed to know if the filmmaker's registration was "active" or something else. Here's the entirety of my override:

{% extends "admin/change_list.html" %}

{% block extrahead %}
{{ block.super }}
<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        $('#result_list tr td:nth-child(7)').each(function() {
            if ($(this).text() != 'active') {
                $(this).css('background-color', 'orange');
            }
        });
    });
})(django.jQuery);
</script>
{% endblock %}

This file is ${TEMPLATE_DIR}/admin/films/film/change_list.html. Django's result list is id'd result_list, all I do here is decorate column 7 with a different background style if the contents of the column are not to my liking.

The admin provides jQuery already, so no additional changes to your application or admin are necessary to make this work.

薄情伤 2024-10-05 22:06:52
class FilmAdmin(admin.ModelAdmin):
 def get_film_status(self, obj):
        if obj.status() != 'active':
            return mark_safe("<span class='row_gray'>%s</span>" % obj.status())

 class Media:
        js = ("js/my_admin.js", )

在 my_admin.js 中:

window.onload = function() {
    var x=document.getElementsByClassName("row_gray");
    var i;
    for (i = 0; i < x.length; i++) {
        var ligne=x[i].parentNode;
        while (ligne.tagName !== "TR") {ligne=ligne.parentNode;}
        ligne.style.backgroundColor = "lightgray";
    }
}
class FilmAdmin(admin.ModelAdmin):
 def get_film_status(self, obj):
        if obj.status() != 'active':
            return mark_safe("<span class='row_gray'>%s</span>" % obj.status())

 class Media:
        js = ("js/my_admin.js", )

and in my_admin.js :

window.onload = function() {
    var x=document.getElementsByClassName("row_gray");
    var i;
    for (i = 0; i < x.length; i++) {
        var ligne=x[i].parentNode;
        while (ligne.tagName !== "TR") {ligne=ligne.parentNode;}
        ligne.style.backgroundColor = "lightgray";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文