簡體   English   中英

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

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

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)

問題是什么?

您的show.Ages屬性是CharField ,而不是IntegerField 不要將其與整數進行比較,而應將其與字符串進行比較,例如

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

正如在評論中指出的上下文變量是shows ,沒有show ,但我懷疑你的模板片斷已經住里面的東西一樣

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

這將創建一個show循環變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM