2

With the following table when returning the BoundColumn it is plaintext and not html.

class CarHistoryTable(tables.Table):

    edit = tables.LinkColumn(
        'Car:update',
        kwargs={'pk': A('id')},
        orderable=False,
        text='Edit'
    )

    def render_edit(self, record, value, bound_column):
        if record.state != Car.NEW:
            return ''
        return super().render_edit()

Ideally I want to return an empty text for Cars that are not NEW state. For other cars I would like to render the edit link.

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
tread
  • 10,133
  • 17
  • 95
  • 170

1 Answers1

2

I understand you assume you can call super().render_edit(), but that's not the case. The logic django-tables2 uses to decide what render-callable it should use looks like this:

If a method render_<columnname> is defined on the table, it is used. In any other case, the render() method of the tables.LinkColumn() instance you assigned to edit while defining your CarHistoryTable is used.

Now, to achieve your goal, I'd do something like this:

class CarUpdateLinkColumn(tables.LinkColumn):
    def render(self, value, record, bound_column):
        if record.state != Car.NEW:
            return ''
        return super().render(value, record, bound_column)


class CarHistoryTable(tables.Table):
    edit = tables.CarUpdateLinkColumn(
        'Car:update',
        kwargs={'pk': A('id')},
        orderable=False,
        text='Edit'
    )

This defines a custom column derived from the LinkColumn you want in case of existing cars. The implementation of render() can call super().render() because such a method actually exists.

tread
  • 10,133
  • 17
  • 95
  • 170
Jieter
  • 4,101
  • 1
  • 19
  • 31
  • I am getting an error `render() got an unexpected keyword argument 'column' from this line: `return super().render(record=record, *args, **kwargs)` – tread Feb 16 '18 at 07:51
  • Ah the signature should be `return super().render(value, record, bound_column)` – tread Feb 16 '18 at 07:56
  • do you use the same method signature as in the above example? – Jieter Feb 16 '18 at 12:37
  • Yes. Check the signature here: https://github.com/jieter/django-tables2/blob/master/django_tables2/columns/linkcolumn.py#L180 – tread Feb 16 '18 at 12:49
  • Yes, but I was under the impression that `*args, **kwargs` would do the magic. Anyway, it works for you now! – Jieter Feb 16 '18 at 12:55