【问题标题】:django-tables2 Display non-table datadjango-tables2 显示非表格数据
【发布时间】:2015-11-21 16:01:49
【问题描述】:

我是 Django 和 Python 的新手,我正在尝试在 django 1.8.4 中使用 django-tables2 1.0.4。

我正在尝试显示非表格数据并显示每个表格行但没有数据。

这是我显示的: 表统计enter image description here

我的代码:

# 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 %}

我得到的不是表名和表数。

【问题讨论】:

  • Sheng Fang,我不确定我是否感谢您的编辑 :-(
  • 你能解释一下Viewclass Totals(View)中是什么
  • @letsc from django.views.generic import View

标签: python python-2.7 django-tables2


【解决方案1】:

所以读到这个页面,数据的格式似乎是错误的。

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

尝试在分配计数字典后立即将其插入该行:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-16
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多