Question

I am using django-tables2 to create my table for me. I need to apply a template tag to each of the cells () in one of the columns. It seems like alot of extra effort to go through and create a custom table layout just to apply the template tag to the one column. Is there a way to do this in django-tables2?

Update:

I may not have explained what I'm looking for well enough. I don't believe that will work.

My code:

class CombineTable(tables.Table):  
    build_no = tables.LinkColumn('run', args=[A('release'), A('id')], verbose_name="Build")  
    flavor = tables.Column(verbose_name="Flavor")  
    pass_rate_pct = tables.Column(verbose_name="Image Pass Rate")

I want each in pass_rate_pct to use the template tag {{pass_rate_color}} in the class () where pass_rate_color then outputs a particular style based upon what the output of pass_rate_pct is.

Was it helpful?

Solution

django_tables2 allows for you to specify an alternative custom template for outputting your tables. Take a copy of django_tables2 / templates / django_tables2 / table.html and rename it e.g. table_pass_rate.html and enter your tag on line 29:

{% pass_rate_color cell %}

Now when generating the table use:

{% render_table table "table_pass_rate.html" %}

See the django_tables2 code for tags and the template for more info.

OTHER TIPS

Try overriding Table.render_FOO method, where foo is the column name, Assuming you have written a custom template tag that takes the column value as an argument. for instance:

import django_tables2 as tables

class SimpleTable(tables.Table):
    custom_row = tables.Column()
    id = tables.Column()
    age = tables.Column()

    def render_custom_row(self, value):
        return '{% pass_rate_color %s %}' % value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top