有一个模型:
class DomainPosition(models.Model):
domain = models.ForeignKey(Domain)
keyword = models.ForeignKey(Keyword)
date = models.DateField()
position = models.IntegerField()
class Meta:
ordering = ['domain', 'keyword']
如果对于每个域我想显示下一个表(表中的数字是位置值),如何获取模板的位置记录:
+----------+--------+--------+-------+--------
| keyword | date1 | date2 | date3 | ...
+----------+--------+--------+-------+--------
| keyword1 | 2 | 6 | 7 | ...
+----------+--------+--------+-------+--------
| keyword2 | 4 | 12 | 5 | ...
+----------+--------+--------+-------+--------
| keyword3 | 6 | 3 | 9 | ...
+----------+--------+--------+-------+--------
其中views.py
:
def show_domain_history(request, domain_name):
domain = Domain.objects.filter(name__contains=domain_name)
if not domain:
return HttpResponseRedirect('/')
else:
# positions = ...
variables = RequestContext(request, {
'domain': domain[0].name,
'positions': positions,
})
return render_to_response('history.html', variables)