django-tables2'表没有显示

时间:2017-11-16 15:16:09

标签: python django django-tables2

我正在使用Django-tables2。我可以使用模型显示表格,但无法使用数据显示表格。我改变了一些东西,但我不确定如何让桌子显示出来 图

def dictfetchall(cursor):
    desc = cursor.description
    return [
        dict(zip([col[0] for col in desc], row))
        for row in cursor.fetchall()
        ]

def Users(request):
    data = dictfetchall(cursor)
    table = ProjectTable(data)
    RequestConfig(request, paginate={"per_page": 4}).configure(data)
    return render(request, 'JiraAdmin/index.html', {'table': table})

table.py
class ProjectTable(tables.Table):
    name = tables.Column(verbose_name='Role')
    lead = tables.Column(verbose_name='Lead')
    display_name = tables.Column(verbose_name='User)

class meta:
    attrs = {'class': 'paleblue'}
    attrs = {'class': 'table table-responsive', 'width': '100%'}

模板

{% render_table ProjectTable %}

更改了我的观点

def Users(request):
    cursor = connection.cursor()
    cursor.execute(select)
    results =  cursor.fetchall()

        x = cursor.description
    resultsList = []
    for r in results:
            i = 0
            d = {}
            while i < len(x):
                d[x[i][0]] = r[i]
                i = i+1
            resultsList.append(d)
    table = ProjectTable(resultsList)
    return render_to_response('index.html', {"table": table})

HTML

<table>
{% for results in resultsList %}
    {% for field, value in results.get_fields %}
        <tr>
           {%render_table ProjectTable%}
        </tr>
    {% endfor %}s
{% endfor %}
</table>

1 个答案:

答案 0 :(得分:1)

我认为我也遇到了同样的问题! Meta类应正确命名,否则渲染会非常突然!

您将类名定为“元”而不是“元”

您的table.py应该看起来像这样!

table.py
class ProjectTable(tables.Table):
    name = tables.Column(verbose_name='Role')
    lead = tables.Column(verbose_name='Lead')
    display_name = tables.Column(verbose_name='User)

    class Meta:
        attrs = {'class': 'paleblue'}
        attrs = {'class': 'table table-responsive', 'width': '100%'}

我知道我在这个线程上还很晚,但是这可能对像我这样的新手很有帮助:) 您能尝试一下,让我知道是否解决了这个问题!