我是django的新手,我尝试根据其值对单元格数据进行样式化,但无法正常工作。 StatusPorumbei模型存储了许多对象,对于每个值,我都要换一个徽章类。如何在StatusPorumbei中获取每个值并将其赋予其他类?首先是模型,其次是模板,最后是视图功能。
class StatusPorumbei(models.Model):
status = models.CharField(max_length=25, null=False, blank=False, unique=True)
def __str__(self):
return self.status
class Meta:
verbose_name = "Status"
verbose_name_plural = "Statusuri"
ordering = ['status']
class Porumbei(models.Model):
id_porumbel = models.AutoField(primary_key=True)
serie_inel = models.CharField(max_length=25, null=False, blank=False, unique=True)
anul = models.CharField(max_length=4, null=False, blank=False)
culoare = models.ForeignKey(CuloriPorumbei, on_delete=models.CASCADE, null=False, blank=False)
culoare_ochi = models.ForeignKey(CuloriOchi, on_delete=models.CASCADE, null=False, blank=False)
sex = models.ForeignKey(Gender, on_delete=models.CASCADE)
ecloziune = models.DateField(null=True, blank=True)
rasa = models.CharField(max_length=50, null=True, blank=True)
linie = models.CharField(max_length=50, null=True, blank=True)
nume = models.CharField(max_length=50, null=True, blank=True)
tata = models.CharField(max_length=25, null=True, blank=True)
mama = models.CharField(max_length=25, null=True, blank=True)
compartiment = models.ForeignKey(Compartimente, on_delete=models.CASCADE, null=False, blank=False)
status = models.ForeignKey(StatusPorumbei, on_delete=models.CASCADE, null=False, blank=False)
<tbody>
{% for item in items %}
<tr class="table-active">
<td>{{ item.serie_inel }}</td>
<td>{{ item.anul }}</td>
<td>{{ item.culoare }}</td>
<td>{{ item.sex }}</td>
<td>{{ item.compartiment }}</td>
<td>{{ item.tata }}</td>
<td>{{ item.mama }}</td>
{% if item.status in sts %}
{% if "Activ" %}
<td><span class="badge badge-success">{{ item.status }}</span></td>
{% elif "Reproducător" %}
<td><span class="badge badge-indigo">{{ item.status }}</span></td>
{% endif %}
{% endif %}
<td>
<a href="#" class="mr-25" data-toggle="tooltip" data-original-title="Editare">
<i class="icon-pencil"></i> </a>
</td>
</tr>
{% endfor %}
</tbody>
def dashboard(request):
items = Porumbei.objects.all()
sts = StatusPorumbei.objects.all()
context = {
'items' : items,
'sts' : sts
}
template = loader.get_template("dashboard.html")
return HttpResponse(template.render(context, request))
答案 0 :(得分:0)
查询item.status
{% if item.status.status == "Activ" %}
<td><span class="badge badge-success">{{ item.status }}</span></td>
{% elif item.status.status == "Reproducător" %}
<td><span class="badge badge-indigo">{{ item.status }}</span></td>
{% else %}
<td><span>{{ item.status.status }}</span></td>
{% endif %}