以下是my models.py:
class Table(models.Model):
choices = (
("BUY", "Buy"),
("HOLD", "Hold"),
("SELL", "Sell"),
)
remarks = models.CharField(max_length=200, choices=choices, default="HOLD", null=True)
有没有办法为每个选项赋予不同的颜色?例如,如果我选择“购买”,它会以一种颜色显示在我的html页面上,比如说蓝色
编辑:
以下是我的完整模型.py:
class Table(models.Model):
choices = (
("BUY", "Buy"),
("HOLD", "Hold"),
("SELL", "Sell"),
)
name = models.CharField(max_length=200, null=True)
date = models.DateTimeField(auto_now_add=False, null=True, editable=True)
buy_call = models.FloatField(null=True)
target_price = models.FloatField(null=True)
stop_loss = models.FloatField(null=True)
remarks = models.CharField(max_length=200, choices=choices, default="HOLD", null=True)
sell_at = models.FloatField(null=True,blank=True)
def __str__ (self):
return self.name
以下是我的观点.py:
def table(request):
table = Table.objects.all().order_by('-date')
return render(request, 'scanner/table.html', {'table':table})
这是我的html:
<tbody>
{% for i in table %}
<tr>
<td>{{i.name}}</td>
<td>{{i.date.date}}</td>
<td>{{i.buy_call}}</td>
<td>{{i.target_price}}</td>
<td>{{i.stop_loss}}</td>
<td><strong>{{i.remarks}}</strong></td>
<td>{{i.sell_at}}</td>
</tr>
{% endfor %}
谢谢
首先,更新views.py,使其将所需的颜色传递给模板
然后,在Django模板中,可以添加以下逻辑:
这将检查
i.remarks
是什么,然后相应地设置颜色旁注:如果要显示买入、卖出、持有而不是买入、卖出、持有,请使用
i.get_remarks_display()
而不是i.remarks
相关问题 更多 >
编程相关推荐