如何给 Django 管理中的一列 Change_list 一个 CSS 类
我想更改 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 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 ofcolumn-<field_name>
on each<th>
element. This can be used to set column widths in a CSS file."虽然这个功能是在 vers1.6 中实现的,正如 StvnW 所说,但对于早期版本,您可以执行以下操作:
在 admin.py 中:
在 mymodel_list.css 中:
指定
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:
In mymodel_list.css:
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 usesth
for the first column of the model, it still counts for atd
child.有一个开放票证,它解决了在change_list视图中为表列指定css类的需要。
也就是说......在票证的描述中,有一个片段在您的change_list-template中注入自定义样式表:
因此您不会覆盖整个模板,仅覆盖您需要的部分(额外样式)。
现在,您可以注入自己的样式表,例如使用 :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:
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