我是 Django 的新手。我试图在
时向表格添加 CSS 样式{% render_table table %}
正在运行。
代码如下:
views.py:
def myTable(request):
table = myDataTable.objects.all()
filter = request.GET.get('q')
startDate = request.GET.get('sd')
endDate = request.GET.get('ed')
if mail:
table = table.filter(Q(filter__icontains=filter) &
Q(evaluation_date__range=[startDate, endDate])).distinct()
else:
table = table.filter(Q(evaluation_date__range=[startDate, endDate])).distinct()
table = TableView(table)
RequestConfig(request).configure(table)
return render(request, 'myapp/myTable.html', {'table': table})
表格.py:
class TableView(tables.Table):
class Meta:
model = myDataTable
template = 'django_tables2/bootstrap.html'
我的应用程序.html
{% load staticfiles %}
{% load render_table from django_tables2 %}
....
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
....
<body>
{% render_table table %}
在project/static/css/
中我有一个自定义样式文件customstyle.css
,但是没有办法让呈现的表格使用那个样式。
你能帮帮我吗?
最佳答案
我努力为我也在渲染的表格获得正确的样式:
{% render_table table %}
在您的 tables.py 文件中,您可以按如下方式设置表本身的属性(例如它的类):
class TableView(tables.Table):
class Meta:
attrs = {'class': 'table table-striped table-hover'}
关于python - 使用 django_tables2 在 django 中的表中添加 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47780834/