python - if 和 elif 在模板 django 中不起作用

标签 python html css django django-templates

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/

相关文章:

Python:如何找到二维点分布的第 n 个分位数

python - 如何定位视频子集的时间戳

javascript - 如何获取<p>标签中的搜索框值 - HTML,JS

html - 负 z-index 有副作用吗

css - 让外部 div 使用 css 滚动过去内部的 div

javascript - 在调整大小时使父级的 div 大小

python - Pandas 删除字符串中的部分空格

python - 安装 scipy 时出现 "failed with error code 1"

javascript - 如何使用javascript调用文本字段中文本更改的函数

css - Chrome : mouse hover on some elements causes vertical scrollbar to jump to the top