[Django]-Conditional statements in Django_tables2 class definition

5👍

You just need to modify how you are returning the table class value.

row_attrs = {
    "class": lambda record: "table-success" if record.status == "0" else ""
}

or as function

def calculate_row_class(**kwargs):
    """ callables will be called with optional keyword arguments record and table 
    https://django-tables2.readthedocs.io/en/stable/pages/column-attributes.html?highlight=row_attrs#row-attributes
    """
    record = kwargs.get("record", None)
    if record:
        return record.status
    return ""

class Meta:
    model = Order

    row_attrs = {
        "class": calculate_row_class
    }

It also looks like you’re not comparing for values correctly, and need to use the double equals instead.

👤Ben

Leave a comment