不能在Django中写出正确的queryset



我有三个模型:subplan,subplanfeature和price。我想显示健身房的价格表,但是显示错误的结果

class SubPlan(models.Model):
title = models.CharField(max_length=150)

def __str__(self):
return self.title

class SubPlanFeature(models.Model):
title = models.CharField(max_length=150)

def __str__(self):
return self.title

class Price(models.Model):
gym=models.ForeignKey(Gym,on_delete=CASCADE)
subplan=models.ForeignKey(SubPlan,on_delete=CASCADE,related_name='subplans')
feature=models.ForeignKey('SubPlanFeature',on_delete=CASCADE,related_name='features')
price = models.IntegerField()
highlight_status = models.BooleanField(default=False,null=True)

这是我的观点

def pricing(request,slug):
gym=models.Gym.objects.get(slug=slug)
price=models.Price.objects.all()
banners=models.Banners.objects.filter(gym=gym)
plans1= models.Price.objects.filter(gender='man',gym=gym)
dfeatures1=models.Price.objects.filter(gender='man',gym=gym)
return render(request, 'pricing.html',{'plans1':plans1,'dfeatures1':dfeatures1,'gym':gym})

和模板

<thead>
<tr>
<th style="width: 34%;"></th>
{% for plan in plans1 %}
<th style="width: 22%;">{{plan.subplan}}</th>
{% endfor %}
</tr>
</thead>
<tbody>

{% for feature in dfeatures1 %}
<tr>
<th scope="row" class="text-start">{{feature.feature}}</th>
{% for plan in plans1 %}
<td>

{{plan.price}} Azn.

</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>

应该显示如下输入图片描述

但是显示了这个

输入图片描述

我应该改变什么?

你可以使用"forloop"模板中的计数器

...        
{% for feature in dfeatures1 %}
<tr>
<th scope="row" class="text-start">{{feature.feature}}</th>
{% for plan in plans1 %}
<td>
# only print when counter in parent loop are the same
{% if forloop.parentloop.counter == forloop.counter %}
{{plan.price}} Azn.
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}

相关内容

  • 没有找到相关文章

最新更新