[Django]-Django-tables2 – how to use a custom filter in a TemplateColumn

9👍

✅

Simplest solution

TemplateColumn renders the column externally to the template. Any custom filters or tags you load in the template won’t be available.

You should be able to load the custom filter when you define the TemplateColumn. Something like:

name1 = tables.TemplateColumn('{% load my_filters %}{{ record.name|int_to_time }}')

Alternative (suggested by Bradley in comments)

Instead of using TemplateColumn in the class defining your table. Use a Column, but define a method render_columnname() with the formatting. Something like:

from myfilters import int_to_time

class MyTable(tables.Table):
    time = tables.Column()

    def render_time(self, value):
        return int_to_time(value)

See Table.render_FOO() Methods for more detail.

Leave a comment