【发布时间】:2021-06-08 19:54:57
【问题描述】:
我想更改表格类中的行颜色,如果发生某些事情,如何放置 html 类。我使用引导程序。
models.py
class Car(models.Model):
name = models.CharField(max_length=20)
color = models.CharField(max_length=20)
views.py
from .models import Car
def index(request):
cars = Car.objects.all()
context = {
'cars' = cars
}
return (request, 'index.html', context)
index.html
<div class="table-responsive-md">
<table class="table tableh-hover">
<thead>
<tr>
<td>Name</td>
<td>Color</td>
</tr>
</thead>
<tbody>
{% for car in cars %}
<tr>
<td>{{ car.name }}</td>
<td {if car.color == red } style="background-color: red;"{% endif %}>{{car.color}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<td {if car.color == red } style="background-color: red;"{% endif %}>{{car.color}}</td>
这条线是我想做的工作
我正在提高我的英语,请耐心等待:D
【问题讨论】:
-
{% if car.color == 'red' %} -
或者你也可以使用
style="background-color: {{ car.color|default:'black' }};"。 -
没用...
-
@WillemVanOnsem 答案对我有用。 1.)您的 if 条件中缺少 '%' 。 2.) 红色应该用引号引起来( "red" )。
标签: python css django django-templates django-staticfiles