Search code examples
pythondjangodjango-tables2

django-tables2 checkbox column name


I've created my table output with django-tables2, but I want a specific name in the table column for the check-boxes.

How can I do that ?

Here is my class for the table drawing, I've modified the sequence of the columns so that my check-box is the first one.

class SimpleTable(tables.Table):
    amend = tables.CheckBoxColumn(verbose_name=('Amend'), accessor='pk')

    class Meta:
        model = SysimpReleaseTracker
        attrs = {"class": "paleblue"}
        listAttrs = list()
        listAttr = list()
        listAttrs = SysimpReleaseTracker._meta.fields

        listAttr.append('amend')
        for x in listAttrs:
            listAttr.append('%s' %x.name)
        sequence = listAttr

Solution

  • CheckBoxColumn has it's own unique rendering for the header and so it will not show the verbose_name unlike all the other columns. You could subclass CheckBoxColumn to change it's behavior though.

    class CheckBoxColumnWithName(tables.CheckBoxColumn):
        @property
        def header(self):
            return self.verbose_name
    
    class SimpleTable(tables.Table):  
        amend = CheckBoxColumnWithName(verbose_name="Amend", accessor="pk")
    

    Alternatively, you could use a TemplateColumn.

    class SimpleTable(tables.Table):  
        amend = TemplateColumn('<input type="checkbox" value="{{ record.pk }}" />', verbose_name="Amend")