Search code examples
django-tables2

Adding additional attributes to the <table> prevents the class attribute from rendering


I am using the Bootstrap template which renders the <table> tag with a class, like this: <table class="table">.

I would like to add an id attribute to the <table> element. I tried this from my views.py:

t = MyTable(data, attrs={'id':'myid'})

This resulted in the id attribute being output as expected but the and the class attribute was removed. Looking at bootstrap.html I see why:

<table {% if table.attrs %} {{ table.attrs.as_html }}{% else %}class="table"{% endif %}>

I see two possible fixes:

  1. Include {"class": "table"} in the attributes array. This should work but I need to remember to do it each time and the view then is doing the job of the template.

  2. Create a new copy of the bootstrap.html template that always outputs class="table" regardless of the attributes passed in. This is better but I'm left wondering why the template author put that 'if' statement in.

Is there a cleaner solution?


Solution

  • Yes, this doesn't have a nice solution currently. I don't like to do 2, but I don't like to require 1 too.

    I'm thinking to solve this by having 'class': 'table' in attrs by default. Just did not have the time to fix this properly yet.