Search code examples
pythondjangodjango-tables2

Humanizing django-tables2 output?


Here's a hack I'm currently using:

class ProductMaxIPPortEldCountMonthlyTable(tables.Table):                      
    ip_count = tables.TemplateColumn('{% load humanize %}{{ record.ip_count|intcomma }} ')

I'm using django.contrib.humanize. Is there a cleaner way to achieve this effect?


Solution

  • You can import the intcomma template filter, and define a custom render method for the column that uses it.

    from django.contrib.humanize.templatetags.humanize import intcomma
    
    class ProductMaxIPPortEldCountMonthlyTable(tables.Table):                      
        ip_count = tables.Column()
    
        def render_ip_count(self, value):
            return intcomma(value)