繁体   English   中英

使用 django_tables2 将 CSS 样式添加到 django 中的表格

[英]Adding CSS style to table in django with django_tables2

我对 Django 很陌生。 我正在尝试向表格添加 CSS 样式时

{% render_table table %}

正在运行。

代码如下所示:

视图.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 ,但没有办法让渲染表使用该样式。

你能帮帮我吗?

文档中解释了如何设置由 django-tables2 生成的表格的样式。 您可以使用默认的类属性,也可以指定自定义的属性。

为了使用您的自定义样式表customstyle.css (使用上面提到的类),您必须将它包含在您的模板中。 django-tables2 不适合你,但你可以从django 教程第 6 部分学习如何做到这一点:

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

您必须根据项目中的位置调整名称和路径。

我努力为我还通过以下方式渲染的表格获得正确的样式:

{% render_table table %}

在您的 tables.py 文件中,您可以执行以下操作来设置表本身的属性(例如其类):

class TableView(tables.Table):
    class Meta:
        attrs = {'class': 'table table-striped table-hover'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM