【问题标题】:write a table with empty cells based on dictionary of values根据值字典编写一个包含空单元格的表格
【发布时间】:2010-10-14 07:56:09
【问题描述】:

我的应用中有这个视图:

def context_detail(request, context_id):
c = get_object_or_404(Context, pk=context_id)
scs = SherdCount.objects.filter(assemblage__context=c).exclude(count__isnull=True)
total = sum(sc.count for sc in scs)
table = []
forms = []
for a in c.assemblage_set.all():
    for sc in a.sherdcount_set.all():
        forms.append(sc.typename)
forms_set = set(forms)
for a in c.assemblage_set.all():
    diko = {}
    diko['assemblage'] = a
    for f in forms_set:
        for sc in a.sherdcount_set.all():
            if f == sc.typename:
                diko[f] = sc.count
            else:
                diko[f] = 0
    table.append(diko)
return render_to_response('tesi/context_detail.html',
    {'context': c, 'total': total, 'sherdcounts': scs, 'table': table, 'forms': forms_set},
    context_instance=RequestContext(request))

两个 for 循环的目的是创建一个字典列表,其中包含 SherdCount.count 值并参考 SherdCount.typename 外键(我能够做到这一点,即使当前代码是有点乱)。

“表格”列表应包含如下内容:

[{<Type: Hayes 61B>: 0, <Type: Hayes 99A-B>: 0, <Type: Hayes 105>: 0, <Type: Hayes 104A>: 0, <Type: Hayes 104B>: 0, <Type: Hayes 103>: 0, <Type: Hayes 91>: 0, <Type: Hayes 91A>: 0, <Type: Hayes 91B>: 0, <Type: Hayes 91C>: 0, <Type: Hayes 91D>: 0, <Type: Hayes 85B>: 0, <Type: Hayes 82A>: 0, <Type: Hayes 76>: 0, <Type: Hayes 73>: 0, <Type: Hayes 72>: 0, <Type: Hayes 70>: 0, <Type: Hayes 68>: 0, <Type: Hayes 67>: 0, <Type: Hayes 66>: 0, <Type: Hayes 62A>: 0, <Type: Hayes 80B>: 0, <Type: Hayes 59>: 0, <Type: Hayes 61A>: 0, <Type: Hayes 91A-B>: 0, <Type: Hayes 58>: 0, <Type: Hayes 50>: 0, <Type: Hayes 53>: 0, <Type: Hayes 71>: 0, <Type: Hayes 60>: 0, <Type: Hayes 80A>: 0, <Type: Hayes Style A2-3>: 0, <Type: Hayes Style B>: 0, <Type: Hayes Style E1>: 1, 'assemblage': <Assemblage: Brescia, Santa Giulia : non periodizzato>}, {<Type: Hayes 61B>: 0, <Type: Hayes 99A-B>: 0, <Type: Hayes 105>: 0, <Type: Hayes 104A>: 0, <Type: Hayes 104B>: 0, <Type: Hayes 103>: 0, <Type: Hayes 91>: 0, <Type: Hayes 91A>: 0, <Type: Hayes 91B>: 0, <Type: Hayes 91C>: 0, <Type: Hayes 91D>: 0, <Type: Hayes 85B>: 0, <Type: Hayes 82A>: 0, <Type: Hayes 76>: 0, <Type: Hayes 73>: 0, <Type: Hayes 72>: 0, <Type: Hayes 70>: 0, <Type: Hayes 68>: 0, <Type: Hayes 67>: 0, <Type: Hayes 66>: 0, <Type: Hayes 62A>: 0, <Type: Hayes 80B>: 0, <Type: Hayes 59>: 0, <Type: Hayes 61A>: 0, <Type: Hayes 91A-B>: 0, <Type: Hayes 58>: 0, <Type: Hayes 50>: 0, <Type: Hayes 53>: 0, <Type: Hayes 71>: 0, <Type: Hayes 60>: 0, <Type: Hayes 80A>: 0, <Type: Hayes Style A2-3>: 0, <Type: Hayes Style B>: 3, <Type: Hayes Style E1>: 0, 'assemblage': <Assemblage: Brescia, Santa Giulia : Periodo IIIA>},

但是很多 0 值显然是错误的。即使可能有一些零(我指的是空单元格)

问题是,一旦我建立了这样一个列表,如何在模板中创建一个包含所有单元格的表格(例如,每个类型 1 行,每个上下文 1 列,在单元格处使用 SherdCount)?

Steko

【问题讨论】:

  • 这将有助于显示结果表结构的示例。
  • 当然。 linux.it/~steko/static/tesi/contexts/9/index.html 这是基本数据的示例(在“Forme”标题下)。我想要paste.pocoo.org/show/108661 之类的东西(天真,但给出了想法)
  • 您能用table 变量的值的样本更新问题吗?
  • 我试图提供更多关于桌子的细节......不知道这是否足以理解我的烂摊子
  • 我找不到第二个维度。您的示例输出显示一个维度是“类型”,另一个维度是“周期”。我在您的 table 数据中看不到 periodo 维度。

标签: python django django-templates


【解决方案1】:

这是数据结构。

[{<Type1>: 16,
  <Type2>: 10,
  <Type3>: 12,
  <Type4>: 7,
  <Type5>: 0,
  'assemblage': <Assemblage1>},
 {<Type1>: 85,
  <Type2>: 18,
  <Type3>: 21,
  <Type4>: 12,
  <Type5>: 2,
  'assemblage': <Assemblage2>},
 ...]

问题是结果表必须按行顺序生成,而这个字典列表是按列顺序的。所以必须将字典列表转为行优先顺序。

from collections import defaultdict
titles = []
cells = defaultdict(list)
for x,col in enumerate(table):
    titles.append( col['assemblage'] )
    for rk in col:
        if rk == 'assemblage': continue # skip the title
        cells[rk][x]= col[rk]

此外,它有助于将结果形式化为列表列表;字典没有内在的顺序。

final= []
for name in sorted( cells.keys() ):
    final.append( cells[name] )

这是将titlesfinal 呈现为表格的模板。

<table>
  <tr>
    {% for t in titles %}<th>{{t}}</th>{% endfor %}
  </tr>
  {% for row in final %}
  <tr>
      {% for cell in row %}<td>{{cell}}</td>{% endfor %}
  </tr>
  {% endfor %}
</table>

【讨论】:

  • 或多或少,它奏效了。我不得不重写部分以前的代码(无论如何都在泄漏)。 defaultdict 很棒..
猜你喜欢
  • 1970-01-01
  • 2023-01-21
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多