문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top