python - 如何在 django 中添加样式
问题描述
我想更改表格类中的行颜色,如果发生某些事情,如何放置 html 类。我使用引导程序。
模型.py
class Car(models.Model):
name = models.CharField(max_length=20)
color = models.CharField(max_length=20)
视图.py
from .models import Car
def index(request):
cars = Car.objects.all()
context = {
'cars' = cars
}
return (request, 'index.html', context)
索引.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 … %}
[Django-doc]用百分号 ( ) 包裹在大括号中{% … %}
,你错过了百分号 (%
);和 - 字符串文字被包裹在引号之间,所以
'red'
, 而不是.red
因此,您可以通过以下方式实现:
<td {% if car.color == 'red' %}style="background-color: red;"{% endif %}>{{car.color}}</td>
推荐阅读
- c++ - 如何从静态类函数初始化静态类对象
- python - 如何从python openpyxl将Pandas Dataframe水平写入Excel工作表(同一行中的元素,连续列)
- python-3.x - “int()的无效文字错误”是什么意思?
- r - 从同一时间戳发生的一组变量中从 R 数据表中选择数据
- regex - 如何在 Azure 逻辑应用中使用正则表达式 (RegEx)
- javascript - 基于 Vue 类的组件无法读取 null 的属性“消息””
- c++ - shared_ptr 的删除器是否存储在自定义分配器分配的内存中?
- javascript - 通过 AJAX 从 javascript 传递到 Python 的变量 - 未找到
- excel - 在 VBA 中查找选定区域的最后一行和最后一列
- pyspark - 从 PySpark 中的日期列计算一年中的一周