【发布时间】: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,我不确定我是否感谢您的编辑 :-(
-
你能解释一下
View在class Totals(View)中是什么 -
@letsc from django.views.generic import View
标签: python python-2.7 django-tables2