Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add column-<field_name> classes to the admin list

This simplifies CSS styling to set column widths.
  • Loading branch information...
commit b7ba4fc408c43ecee385d2ca3582697ec54ac8a6 1 parent 8503120
Diederik van der Boor authored February 24, 2013
6  django/contrib/admin/templatetags/admin_list.py
@@ -12,10 +12,9 @@
12 12
 from django.utils import formats
13 13
 from django.utils.html import format_html
14 14
 from django.utils.safestring import mark_safe
15  
-from django.utils import six
16 15
 from django.utils.text import capfirst
17 16
 from django.utils.translation import ugettext as _
18  
-from django.utils.encoding import smart_text, force_text
  17
+from django.utils.encoding import force_text
19 18
 from django.template import Library
20 19
 from django.template.loader import get_template
21 20
 from django.template.context import Context
@@ -112,12 +111,13 @@ def result_headers(cl):
112 111
                 # Not sortable
113 112
                 yield {
114 113
                     "text": text,
  114
+                    "class_attrib": format_html(' class="column-{0}"', field_name),
115 115
                     "sortable": False,
116 116
                 }
117 117
                 continue
118 118
 
119 119
         # OK, it is sortable if we got this far
120  
-        th_classes = ['sortable']
  120
+        th_classes = ['sortable', 'column-{0}'.format(field_name)]
121 121
         order_type = ''
122 122
         new_order_type = 'asc'
123 123
         sort_priority = 0
7  docs/ref/contrib/admin/index.txt
@@ -543,6 +543,13 @@ subclass::
543 543
       The above will tell Django to order by the ``first_name`` field when
544 544
       trying to sort by ``colored_first_name`` in the admin.
545 545
 
  546
+    * .. versionadded:: 1.6
  547
+
  548
+      The field names in ``list_display`` will also appear as CSS classes in
  549
+      the HTML output, in the form of ``column-<field_name>`` on each ``<th>``
  550
+      element. This can be used to set column widths in a CSS file for example.
  551
+
  552
+
546 553
 .. attribute:: ModelAdmin.list_display_links
547 554
 
548 555
     Set ``list_display_links`` to control which fields in ``list_display``
3  docs/releases/1.6.txt
@@ -101,6 +101,9 @@ Minor features
101 101
   context through to feed templates using a new `Feed.get_context_data()`
102 102
   callback.
103 103
 
  104
+* The admin list columns have a ``column-<field_name>`` class in the HTML
  105
+  so the columns header can be styled with CSS, e.g. to set a column width.
  106
+
104 107
 Backwards incompatible changes in 1.6
105 108
 =====================================
106 109
 

0 notes on commit b7ba4fc

Please sign in to comment.
Something went wrong with that request. Please try again.