Question

In django-tables2 is it possible to set the template_name for a TemplateColumn inside the Table's constructor? I would like to be able to chose the template at runtime.

From the docs the only way to set the template is like this:

class ExampleTable(tables.Table):
    foo = tables.TemplateColumn('{{ record.bar }}')
    # contents of `myapp/bar_column.html` is `{{ value }}`
    bar = tables.TemplateColumn(template_name='myapp/name2_column.html')

In my app there are different templates for one column depending on the state for the app and I would like to avoid creating a different Table class for each state and instead just swap the template at runtime.

Was it helpful?

Solution

You can try to change template at runtime in this way:

e  = ExampleTable( your_query )
e.columns['bar'].column.template_name = 'your_template'

Disclaimer: Not tested. Please, test you it and come back. I will delete answer if don't run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top