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

落花浮王杯 提交于 2019-12-22 07:01:39

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/13289848/is-it-possible-to-apply-a-template-tag-to-a-td-when-using-django-tables2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!