将附加属性添加到 <table> 会阻止呈现 class 属性
Adding additional attributes to the <table> prevents the class attribute from rendering
我正在使用 Bootstrap 模板,它使用 class 呈现 <table>
标签,如下所示:<table class="table">
.
我想向 <table>
元素添加一个 id 属性。我从 views.py:
试过了
t = MyTable(data, attrs={'id':'myid'})
这导致 id 属性按预期输出,但 class 属性被删除。看着 bootstrap.html 我明白了为什么:
<table {% if table.attrs %} {{ table.attrs.as_html }}{% else %}class="table"{% endif %}>
我看到两个可能的修复方法:
在属性数组中包含 {"class": "table"}
。这应该可行,但我需要记住每次都这样做,然后视图正在执行模板的工作。
创建 bootstrap.html 模板的新副本,无论传入的属性如何,它始终输出 class="table"
。这样更好,但我想知道为什么模板作者将 'if' 语句放入
是否有更清洁的解决方案?
是的,目前没有很好的解决方案。我不喜欢做 2,但我也不喜欢要求 1。
我想通过在 attrs
中默认设置 'class': 'table'
来解决这个问题。只是还没有时间正确解决这个问题。
我正在使用 Bootstrap 模板,它使用 class 呈现 <table>
标签,如下所示:<table class="table">
.
我想向 <table>
元素添加一个 id 属性。我从 views.py:
t = MyTable(data, attrs={'id':'myid'})
这导致 id 属性按预期输出,但 class 属性被删除。看着 bootstrap.html 我明白了为什么:
<table {% if table.attrs %} {{ table.attrs.as_html }}{% else %}class="table"{% endif %}>
我看到两个可能的修复方法:
在属性数组中包含
{"class": "table"}
。这应该可行,但我需要记住每次都这样做,然后视图正在执行模板的工作。创建 bootstrap.html 模板的新副本,无论传入的属性如何,它始终输出
class="table"
。这样更好,但我想知道为什么模板作者将 'if' 语句放入
是否有更清洁的解决方案?
是的,目前没有很好的解决方案。我不喜欢做 2,但我也不喜欢要求 1。
我想通过在 attrs
中默认设置 'class': 'table'
来解决这个问题。只是还没有时间正确解决这个问题。