title: How do I customize a column based on its value?
body: <p>Using django-tables2 is very easy from your model to get a table. In my case I need one of my table columns to be formated based on its value.</p>
  
  <p>In my current html table it would look like below:</p>
  
  <pre><code>{% if record.status|stringformat:"s" == "New" %}
      &lt;td class="bg-success&gt;&lt;/td&gt;
  </code></pre>
  
  <p>If the value is New the cell background should be green.</p>
  
  <p>From what I could find there are maybe 3 ways to do something like this:</p>
  
  <p><strong>Updated solutions:</strong></p>
  
  <p>1.Create a class and a css rule with the appropriate background color and add it to the column:</p>
  
  <pre><code>class MyTable(tables.Table):    
      status = tables.Column(attrs={"class": lambda record: record.status})
  
  .New {
  background-color: green
  }
  </code></pre>
  
  <p>This way it works, although I think that <code>record.status</code> could work, without the lambda.</p>
  
  <p>2.You can specify how to render a column:</p>
  
  <pre><code>class MyTable(tables.Table):    
      status = tables.Column()
  
      def render_status(self, value):
          if value.name == "New":
              change class accordingly
  </code></pre>
  
  <p>The change class accordingly part eludes me.</p>
  
  <p>Also you can create a custom column:</p>
  
  <pre><code>class StatusColumn(tables.Column):
      def render(self, value):
          if value == "New":
              return format_html('&lt;span class="text-success"&gt;{}&lt;/span&gt;', value)
  </code></pre>
  
  <p>I made this work using a span tag to pass a bootstrap class to format the cell.</p>
  
  <p>3.Use a TemplateColumn and pass html:</p>
  
  <pre><code>class MyTable(tables.Table):  
      status = tables.TemplateColumn("""
      {% if record.status|stringformat:"s" == "New" %}
          &lt;td class="bg-success"&gt;&lt;/th&gt;
      {% else %}
          &lt;td class="bg-danger"&gt;&lt;/th&gt;
      {% endif %}
      """)
  </code></pre>
  
  <p>This way a new column is created formated correctly.</p>
  
  <p>I am still searching how to do this and I would appreciate any help.</p>