django 即使条件为真,Jinja elif也不工作

t9eec4r0  于 2022-12-01  发布在  Go
关注(0)|答案(1)|浏览(153)

第四个elif语句是导致我这个问题的原因。我已经把第三个elif语句和第四个交换了,每次第四个在第三位时它都能工作。

{% block content%}

{% load static %}
<link rel="stylesheet" href="{% static 'css/home_page.css' %}">
<link rel="stylesheet" href="{% static 'css/home_w_d_cs.css' %}">

{% if first_hour_d == 'clear sky' and time_of_day == True %} <!-- day == True means day -->

    <div class="side-hour-icon">
        <img src="{% static 'images/sunny-black.png' %}" alt="" width="55" height="50">
    </div>

{% elif first_hour_d == 'clear sky' and time_of_day == False %} <!-- day == False means night -->
    <div class="side-hour-icon">
        <img src="{% static 'images/clear-night-black.png' %}" alt="" width="55" height="50">
    </div>

{% elif first_hour_d == 'overcast clouds' or 'broken clouds' %}
    <div class="side-hour-icon">
        <img src="{% static 'images/cloudy2.png' %}" alt="" width="55" height="50">
    </div>
    
{% elif first_hour_d == 'few clouds' or 'scattered clouds' %}
    <div class="side-hour-icon">
        <img src="{% static 'images/few-clouds-black.png' %}" alt="" width="55" height="50">
    </div>

{% endif %}

{% endblock %}

我想有几个elif语句,也许10个或12个。这可能吗?

2skhul33

2skhul331#

您不能执行以下操作:

{% elif first_hour_d == 'overcast clouds' or 'broken clouds' %}

因为第二个字符串的计算结果总是True
您必须:

{% elif first_hour_d == 'overcast clouds' or first_hour_d == 'broken clouds' %}

相关问题