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.

Était-ce utile?

La 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top