0
我試圖將「信息」放入一個表格中,該表格有稱爲「類別」和「樣式」的行和列。下面顯示了我的模型和視圖。如何將2個模型中的django對象放置到模板表中?
model.py
class Headings(models.Model):
categories = models.CharField(max_length=20)
stylings = models.CharField(max_length=20)
class Information(models.Model):
headings = models.ForeignKey(Headings)
info = models.CharField(max_length=100)
views.py
class InformationListView(ListView):
model = Information
def get_context_data(self, **kwargs):
context = super(InformationListView, self).get_context_data(**kwargs)
context['categories_ordered'] = Headings.objects.order_by('categories')
context['stylings_ordered'] = Headings.objects.order_by('stylings')
return context
到目前爲止,我已經能夠開始我的模板表
<table>
<tr>
<th>Styles</th>
{% for cols in categories_ordered %}
<th class="rotate"><div><span>{{ cols.categories }}</span></div>
{% endfor %}
</th>
</tr>
{% for row in stylings_ordered %}
<tr>
<td>{{ row.stylings }}</td>
{% for col in categories_ordered %}
<td>
... need algorithm that places the correct info in this cell. {{ Information.info }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
可能有6個不同的類別和6分不同的兩種風格,但也許只有4信息的對象。所以困難的部分(imo)將信息放在表格中正確的單元格中。我嘗試了不同的東西,但似乎沒有接近。我想這是一個相當普遍的問題,一直在解決。
是的,但只是放在信息到表中從左至右所有一行。信息不一定與標題對齊。假設類別按以下順序輸入:運動,愛好,繪畫,陶器,攝影,滑雪。假設只有一種風格。那麼也許只有兩個信息輸入,一個是繪畫,一個是滑雪。表格應該跳過前兩列,填寫繪畫,跳過陶器和攝影專欄,然後填寫滑雪專欄。當行和列都必須匹配時,它變得更加複雜。 –
請檢查我的回答請 – Jayground
是的,這看起來像我想要做的。 –