Reputation: 221
I'm very new to Django. I'm trying to add a CSS style to a table when
{% render_table table %}
is being run.
The code looks as following:
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})
tables.py:
class TableView(tables.Table):
class Meta:
model = myDataTable
template = 'django_tables2/bootstrap.html'
myApp.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 %}
In project/static/css/
I have a custom style file customstyle.css
, but there is no way to make the rendered table use that style.
Could you please help me?
Upvotes: 3
Views: 5786
Reputation: 31
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'}
Upvotes: 3
Reputation: 4229
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.
Upvotes: 0