[Django]-Adding CSS style to table in django with django_tables2

3👍

I struggled to get the correct style for a table I was also rendering via:

{% render_table table %}

In your tables.py file you can do as follows to set attributes of the table itself (such as its’ class):

class TableView(tables.Table):
    class Meta:
        attrs = {'class': 'table table-striped table-hover'}
👤Cody M

0👍

Styling a table generated by django-tables2 is explained in the documentation. You can use the default class attributes, or specify custom ones.

In order to use your custom style sheet customstyle.css (using the classes mentioned above), you must include it into your template. django-tables2 doesn’t do that for you, but you can learn how to do that from the django tutorial part 6:

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

You have to adjust names and paths according to the location in your project.

👤Jieter

Leave a comment