在我的rider_detail.html中,我应该能够看到带有骑手得分的表格,但我看不到
我的模型。py:
class Rider(models.Model):
id_rider = models.IntegerField(default = 0,blank= True, primary_key=True)
[...]
def __str__(self):
return self.display_name
class Risultato(models.Model):
TYPE_CHOICES = (('SR','sr'),('ITT', 'itt'),('HC', 'hc'),('1C','1c'),('TTT','ttt'))
id_ris = models.IntegerField(default = 0,blank= True, primary_key=True)
id_rider = models.ForeignKey(Rider,related_name='rider', on_delete=models.CASCADE)
id_stage = models.ForeignKey(Stage,related_name='team', on_delete=models.CASCADE)
type_ris = models.CharField(choices=TYPE_CHOICES,max_length = 256,blank= True)
rank = models.IntegerField(default = 0,blank= True)
punti = models.IntegerField(default = 0,blank= True)
def __str__(self):
return str(self.id_ris)
我的view.py:
class RiderDetails(DetailView):
model = Rider
template_name = 'game/rider_detail.html'
def get_context_data(self, *args, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data['risultato_qs'] = Risultato.objects.filter(id_rider=self.object)
return context_data
我的rider_detail.html
{% extends 'game/base.html'%}
{% block content %}
<h1>{{ rider.display_name }}</h1>
<p>
<h3>age:</h3>{{rider.age}}
<h3>nationality:</h3>{{rider.nationality}}
<h3>height:</h3>{{rider.height}}
<h3>weight:</h3>{{rider.weight}}
<h3>team:</h3>{{rider.team}}
</p>
<h3></h3>
<h2> Score history</h2>
<table>
<tbody>
{% for risultato in risultato_qs %}
<tr>
{{ risultato }}
<td>{{ risutato.type_ris }}</td>
<td>{{ risutato.punti }}</td>
... etc ...
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
我已经安装了Django Debug Toolbar,以查看查询是否正确。实际上,我们看到2个结果,一个结果为100分(“蓬蒂”),另一个结果为50分。
答案 0 :(得分:0)
在for循环中,您使用的是risutato
而不是risultato
。更改为:
{% for risultato in risultato_qs %}
<tr>
<td>{{ risultato.type_ris }}</td>
<td>{{ risultato.punti }}</td>
</tr>
{% endfor %}
答案 1 :(得分:0)
您的循环中有错别字。您可以将loop
更改为此:
rider_detail.html
{% for risutato in risultato_qs %}
<tr>
{{ risutato }}
<td>{{ risutato.type_ris }}</td>
<td>{{ risutato.punti }}</td>
... etc ...
</tr>
{% endfor %}
或者按照Alasdair中指定的相反方法。