更改 Django 管理列表中的行颜色
我想根据模型中名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我错了,还有另一种选择,仅使用 Django 管理应用程序。
在应用程序的 admin.py 中,您可以为表格单元格的内容定义渲染器。这是我的电影库的变体:
在这里,我创建了一个 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:
Here, I've created a field name,
film_status
, that does not exist in the Film model, and defined it as a method ofFilmAdmin
. It gets passed the item for every row. I've had to tell the renderer toallow_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.
查看django-liststyle,这正是您所追求的。
Check out django-liststyle, exactly what you're after.
事实证明,自定义change_list_results.html 和关联的生成器是相当困难的。我想提出一个完全不同的解决方案:为您的应用程序覆盖change_list.html,并使用Javascript来实现您想要的效果。
我遇到了与你完全相同的问题。对于电影库,我需要知道电影制片人的注册是否“有效”或其他。这是我的覆盖的全部内容:
此文件是
${TEMPLATE_DIR}/admin/films/film/change_list.html
。 Django 的结果列表是 id'dresult_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:
This file is
${TEMPLATE_DIR}/admin/films/film/change_list.html
. Django's result list is id'dresult_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.
在 my_admin.js 中:
and in my_admin.js :