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>
View .py
def index(request):
shows = show.objects.all()
context = {
'shows':shows
}
return render(request,'index.html', context)
模型.py
class show(models.Model):
Ages = models.CharField(max_length=10,default='',null=True)
问题是什么?
最佳答案
您的 show.Ages
属性是 CharField
,而不是 IntegerField
。不要将其与整数进行比较,而应该将其与字符串进行比较,例如
{% if show.Ages == '17' %}...{% endif %}
正如评论中所指出的,您的上下文变量是 shows
,而不是 show
,但我怀疑您的模板片段已经存在于类似的内容中
{% for show in shows %}...{% endfor %}
这将创建一个show
循环变量。
关于python - if 和 elif 在模板 django 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46263989/