Note that there are some explanatory texts on larger screens.

plurals
  1. POwrite a table with empty cells based on dictionary of values
    text
    copied!<p>I have this view in my app:</p> <pre><code>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)) </code></pre> <p>The aim of the two for loops would be that of creating a list of dictionaries that holds values of SherdCount.count with reference to the SherdCount.typename foreign key (and I was able to do that, even if the current code is a bit messed up).</p> <p>The "table" list should contain something like this:</p> <pre><code>[{&lt;Type: Hayes 61B&gt;: 0, &lt;Type: Hayes 99A-B&gt;: 0, &lt;Type: Hayes 105&gt;: 0, &lt;Type: Hayes 104A&gt;: 0, &lt;Type: Hayes 104B&gt;: 0, &lt;Type: Hayes 103&gt;: 0, &lt;Type: Hayes 91&gt;: 0, &lt;Type: Hayes 91A&gt;: 0, &lt;Type: Hayes 91B&gt;: 0, &lt;Type: Hayes 91C&gt;: 0, &lt;Type: Hayes 91D&gt;: 0, &lt;Type: Hayes 85B&gt;: 0, &lt;Type: Hayes 82A&gt;: 0, &lt;Type: Hayes 76&gt;: 0, &lt;Type: Hayes 73&gt;: 0, &lt;Type: Hayes 72&gt;: 0, &lt;Type: Hayes 70&gt;: 0, &lt;Type: Hayes 68&gt;: 0, &lt;Type: Hayes 67&gt;: 0, &lt;Type: Hayes 66&gt;: 0, &lt;Type: Hayes 62A&gt;: 0, &lt;Type: Hayes 80B&gt;: 0, &lt;Type: Hayes 59&gt;: 0, &lt;Type: Hayes 61A&gt;: 0, &lt;Type: Hayes 91A-B&gt;: 0, &lt;Type: Hayes 58&gt;: 0, &lt;Type: Hayes 50&gt;: 0, &lt;Type: Hayes 53&gt;: 0, &lt;Type: Hayes 71&gt;: 0, &lt;Type: Hayes 60&gt;: 0, &lt;Type: Hayes 80A&gt;: 0, &lt;Type: Hayes Style A2-3&gt;: 0, &lt;Type: Hayes Style B&gt;: 0, &lt;Type: Hayes Style E1&gt;: 1, 'assemblage': &lt;Assemblage: Brescia, Santa Giulia : non periodizzato&gt;}, {&lt;Type: Hayes 61B&gt;: 0, &lt;Type: Hayes 99A-B&gt;: 0, &lt;Type: Hayes 105&gt;: 0, &lt;Type: Hayes 104A&gt;: 0, &lt;Type: Hayes 104B&gt;: 0, &lt;Type: Hayes 103&gt;: 0, &lt;Type: Hayes 91&gt;: 0, &lt;Type: Hayes 91A&gt;: 0, &lt;Type: Hayes 91B&gt;: 0, &lt;Type: Hayes 91C&gt;: 0, &lt;Type: Hayes 91D&gt;: 0, &lt;Type: Hayes 85B&gt;: 0, &lt;Type: Hayes 82A&gt;: 0, &lt;Type: Hayes 76&gt;: 0, &lt;Type: Hayes 73&gt;: 0, &lt;Type: Hayes 72&gt;: 0, &lt;Type: Hayes 70&gt;: 0, &lt;Type: Hayes 68&gt;: 0, &lt;Type: Hayes 67&gt;: 0, &lt;Type: Hayes 66&gt;: 0, &lt;Type: Hayes 62A&gt;: 0, &lt;Type: Hayes 80B&gt;: 0, &lt;Type: Hayes 59&gt;: 0, &lt;Type: Hayes 61A&gt;: 0, &lt;Type: Hayes 91A-B&gt;: 0, &lt;Type: Hayes 58&gt;: 0, &lt;Type: Hayes 50&gt;: 0, &lt;Type: Hayes 53&gt;: 0, &lt;Type: Hayes 71&gt;: 0, &lt;Type: Hayes 60&gt;: 0, &lt;Type: Hayes 80A&gt;: 0, &lt;Type: Hayes Style A2-3&gt;: 0, &lt;Type: Hayes Style B&gt;: 3, &lt;Type: Hayes Style E1&gt;: 0, 'assemblage': &lt;Assemblage: Brescia, Santa Giulia : Periodo IIIA&gt;}, </code></pre> <p>But the many 0 values are obviously wrong. even though there might be some zeros (the empty cells I was referring to)</p> <p>The question is, once I have built such a list, how do I create a table in the template with all the cells (e.g. 1 row per Type and 1 column per Context, with SherdCount at cells) ?</p> <p>Steko</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload