How to give a column in the Django admin change_list a CSS class

后端 未结 3 1963
梦如初夏
梦如初夏 2021-01-06 09:44

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 overwri

3条回答
  •  时光说笑
    2021-01-06 10:46

    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 }}
      
    {% 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

提交回复
热议问题