简体   繁体   English

django-tables2显示非表数据

[英]django-tables2 Display non-table data

I'm new to Django and Python and am trying to use django-tables2 1.0.4 in django 1.8.4. 我是Django和Python的新手,正在尝试在Django 1.8.4中使用django-tables2 1.0.4。

I'm trying to display non-table data and getting the each table line displayed but without data. 我正在尝试显示非表数据并显示每个表行但没有数据。

This is what I display: Table Statistics enter image description here 我将显示以下内容:表统计信息在此处输入图像描述

My code: 我的代码:

# tables.py
class TotalTable(tables.Table):

    tbl = tables.Column(orderable=True,verbose_name='Table Name')
    tbl_cnt = tables.Column(orderable=True, verbose_name='Table Count')

    class Meta:
        # add class="paleblue: to <table> tag
        attrs = {"class": "paleblue", "type": "dict"}
        fields = ('tbl', 'tbl_cnt')
        sequence = fields
        order_by = ('tbl',)

# views.py
class Totals(View):

    template_name = 'lib/totals.html'

    counts = {'Authors': Author.objects.all().count(),
              .........etc......
              'Titles': Title.objects.all().count()
            }
    table = TotalTable(counts)

    def get(self, request):
        RequestConfig(request).configure(self.table)
        return render(request, self.template_name, {'table': self.table})

#urls.py
urlpatterns = [
               url(r'^totals/$', Totals.as_view()),
]
# totals.html
{% extends "lib/base.html" %}
{% load render_table from django_tables2 %}

{% block sidebar %}
{% endblock %}

{% block content %}

  <h1>Table Statistics</h1>
  {% render_table table %}

{% endblock %}

Instead of table name and table count I'm getting '— —'. 我得到的不是表名和表数,而是“ — —”。

So reading this page, the format of the data appears to be wrong. 因此,阅读此页面后,数据格式似乎是错误的。

https://django-tables2.readthedocs.org/en/latest/pages/table-data.html https://django-tables2.readthedocs.org/en/latest/pages/table-data.html

Try inserting this into the line immediately after assigning the counts dict: 分配计数dict后,尝试立即将其插入行中:

counts = [ { 'tbl': k, 'tbl_cnt': v} for k,v in counts.items() ]

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

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