Is it possible to apply a template tag to a <td> when using django-tables2?
Asked Answered
F

2

6

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.

Fidgety answered 8/11, 2012 at 13:29 Comment(0)
U
2

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.

Unasked answered 11/4, 2013 at 23:8 Comment(1)
This is how I went about it, however I had to do an if statement in there so that it only did pass_rate_color on a certain column, however it did work. Thanks.Fidgety
W
0

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
Wessling answered 8/11, 2012 at 19:59 Comment(2)
on writing custom template tags see docs.djangoproject.com/en/dev/howto/custom-template-tagsWessling
The templatetag already exists. I'm trying to figure out how to pass it through tables.py.. Currently it shows up in the source as plain text instead of the templatetag actually being run.Fidgety

© 2022 - 2024 — McMap. All rights reserved.