【发布时间】:2018-02-26 02:05:52
【问题描述】:
if 和 elif 在我的模板 django 中不起作用
index.html
<a style="{% if show.Ages == 19 %}background:#ff3636;{% elif show.Ages == 17 %}background:#fb9c92;{% elif show.Ages == 13 %}background:#ffb466;{% else %}background:#4aff68;{% endif %};border-radius: 15px;width: 140px;height: 42;margin-right: 831px;margin-top: -200;" class="button">رده سنی:+{{ show.Ages }}</a>
views.py
def index(request):
shows = show.objects.all()
context = {
'shows':shows
}
return render(request,'index.html', context)
models.py
class show(models.Model):
Ages = models.CharField(max_length=10,default='',null=True)
有什么问题?
【问题讨论】:
-
你为什么缺少冒号
:? -
这是
shows不是show。 -
这与您的问题无关,但请考虑将
show重命名为Show并将Ages重命名为ages。当 Python 开发人员看到一个大写字母时,他们认为是“类”,而不是“属性”。 official style guide 包含其他建议。 -
{% if show.Ages == 19 %} to {% if show.Ages == "19" %} 谢谢
标签: python html css django django-templates