简体   繁体   English

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

[英]if and elif doesn't work in template django

if and elif doesn't work in my template django if和elif在我的模板django中不起作用

index.html 的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 views.py

def index(request):
    shows = show.objects.all()
    context = {
        'shows':shows
    }
    return render(request,'index.html', context)

models.py models.py

class show(models.Model):
    Ages = models.CharField(max_length=10,default='',null=True)

what is the problem? 问题是什么?

Your show.Ages attribute is a CharField , not an IntegerField . 您的show.Ages属性是CharField ,而不是IntegerField Instead of comparing it against integers you should compare it against strings, eg 不要将其与整数进行比较,而应将其与字符串进行比较,例如

{% if show.Ages == '17' %}...{% endif %}

As pointed out in the comments your context variable is shows , not show , but I suspect that your template snippet already lives inside something like 正如在评论中指出的上下文变量是shows ,没有show ,但我怀疑你的模板片断已经住里面的东西一样

{% for show in shows %}...{% endfor %}

which would create a show loop variable. 这将创建一个show循环变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM