如何给 Django 管理中的一列 Change_list 一个 CSS 类

发布于 2024-11-02 09:24:57 字数 81 浏览 3 评论 0原文

我想更改 Django 管理列表显示中的列宽。

是否可以以某种方式将 CSS 类名添加到列中? 我最好不要覆盖整个模板来完成此任务。

I'd like to change the column widths in the list display of the Django admin.

Is it possible somehow to add a CSS classname to a column?
I'd preferably not overwrite the entire template to accomplish this.

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

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

发布评论

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

评论(3

唯憾梦倾城 2024-11-09 09:24:57

在 Django >= 1.6 中,CSS 类包含在 list_display 输出中:

list_display 中的字段名称也会以 CSS 类的形式出现在 HTML 输出中,以 column 的形式出现- 每个 元素上的 这可用于设置 CSS 文件中的列宽。

In Django >= 1.6, CSS classes are included with list_display output:

"The field names in list_display will also appear as CSS classes in the HTML output, in the form of column-<field_name> on each <th> element. This can be used to set column widths in a CSS file."

在你怀里撒娇 2024-11-09 09:24:57

虽然这个功能是在 vers1.6 中实现的,正如 StvnW 所说,但对于早期版本,您可以执行以下操作:

在 admin.py 中:

class MyModelAdmin(admin.ModelAdmin):
    # your code here

    # specify a stylesheet just for the list view
    class Media:
        css = {'all': ('css/mymodel_list.css')}

在 mymodel_list.css 中:

/* replace '5' with the column desired */
table#result_list td:nth-child(5) {
    width: 15em;
}

指定 table#result_list 将仅将此样式表应用于列表视图,不会影响该模型的正常管理页面。另请注意,虽然 django 使用 th 作为模型的第一列,但它仍然对 td 子项有效。

While this feature is implemented in vers1.6 as StvnW said, for earlier versions you can do the following:

In admin.py:

class MyModelAdmin(admin.ModelAdmin):
    # your code here

    # specify a stylesheet just for the list view
    class Media:
        css = {'all': ('css/mymodel_list.css')}

In mymodel_list.css:

/* replace '5' with the column desired */
table#result_list td:nth-child(5) {
    width: 15em;
}

Specifying table#result_list will apply this stylesheet only to the list view and won't affect the normal admin page for this model. Also, note that while django uses th for the first column of the model, it still counts for a td child.

酸甜透明夹心 2024-11-09 09:24:57

有一个开放票证,它解决了在change_list视图中为表列指定css类的需要。

也就是说......在票证的描述中,有一个片段在您的change_list-template中注入自定义样式表:

{% extends "admin/change_list.html" %}
{% block extrastyle %}
  {{ block.super }}
  <link rel="stylesheet" type="text/css" href="/css/poll_admin_changelist.css" />
{% endblock extrastyle %}

因此您不会覆盖整个模板,仅覆盖您需要的部分(额外样式)。

现在,您可以注入自己的样式表,例如使用 :nth-child-selector

另一种选择是将您的特定字段包装在 html 中,这可以使用 list_display 选项。在这里,您可以为包装元素定义宽度或类。
不过,如果您想控制有限的字段集的宽度,这才有意义

There is an open ticket that addresses the need for specifying css classes for table columns in the change_list view.

That said ... in the description for the ticket there's a snippet that injects a custom stylesheet in your change_list-template:

{% extends "admin/change_list.html" %}
{% block extrastyle %}
  {{ block.super }}
  <link rel="stylesheet" type="text/css" href="/css/poll_admin_changelist.css" />
{% endblock extrastyle %}

So you don't override the whole template, only the part (extrastyle) you need.

Now you could inject your own stylesheet and for example style your columns using the :nth-child-selector

Another option would be to wrap your specific fields in html which can be done using the list_display option. Here you could define a width or class for a wrapped element.
This does only makes sense though, if you want to control the width of limited set of fields

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文