python - 使用 django_tables2 在 django 中的表中添加 CSS 样式

标签 python django python-3.x django-tables2

我是 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/

相关文章:

python - AWS - 运行网络服务 - Cherrypy + Python

python - Django 模板列表未在 for 循环中打印

django - 如何在 django 中使用 pdf.js

python - 如何使用 eyed3 模块从 Mp3 中删除现有的专辑封面图像

python - python中的树状图

Python 访问 BaseRequestHandler

Django ModelChoiceField 使用来自一个模型属性的不同值

django - 在 Django 中保存外键相关表单

python-3.x - 以 3D 方式可视化链接攻击的最佳方式是什么

python - 如何解决 : Python import of file with . csv Dictreader 在未定义字符上失败