本文介绍了如何使用django-tables2从dict创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下的dict列表:
I have a list of dict that look like this:
[set([u'meal', '0:08:35.882945']),
set([0, u'personal']),
set([0, u'sleep']),
set([0, u'transport']),
set([0, u'work'])]
我做的是:
[u'meal',u'personal', u'sleep', u'transport', u'work']
['0:08:35.882945', 0, 0, 0, 0]
使用此命令:
nob = [{m,n} for m,n in zip(cats,tot3)]
我该怎么把它变成一个表?
How can I turn this into a django-tables2 table?
我尝试过:
# tables.py
class Small_table (tables.Table):
category = tables.Column(verbose_name="category")
class Meta:
attrs = {"class": "paleblue"}
# views.py
nt = Small_table(nob)
RequestConfig(request).configure(nt)
但是表中有一列破折号而不是我的数据,应该怎么改?
But the table has one column of dashes rather than my data, what should I change?
推荐答案
这是我最后做的:
table.py:
class Small_table (tables.Table):
name = tables.Column(verbose_name="category",order_by="name")
tot = tables.Column(orderable=False)
class Meta:
attrs = {"class": "paleblue"}
在我看来
from .tables import Small_table
from django_tables2 import RequestConfig
nob = [{"name":m,"tot":n} for m,n in zip(cats,tot3)]
nt = Small_table(nob)
RequestConfig(request).configure(nt)
return render(request, "job/job_home.html", { "small":nt })
在模板中:
{% load render_table from django_tables2 %}
<link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue
{% render_table small %}
这篇关于如何使用django-tables2从dict创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!