У меня есть этот вид в моем приложении:написать таблицу с пустыми ячейками на основе словаря значений
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))
Цель два для петель будет то, что создание списка словарей, который содержит значение 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
Это поможет показать образец результирующей структуры таблицы. –
Несомненно. http://www.linux.it/~steko/static/tesi/contexts/9/index.html вот пример базовых данных (под заголовком «Forme»). Я хотел бы иметь что-то вроде http://paste.pocoo.org/show/108661/ (наивно, но дает идею) – steko
Можете ли вы обновить вопрос образцом переменной 'table'? –