【问题标题】:Jinja elif not working even though condition is true即使条件为真,Jinja elif 也无法正常工作
【发布时间】:2022-11-29 02:57:15
【问题描述】:

第四个 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。这可能吗?

【问题讨论】:

  • 确实很少有语言接受像first_hour_d == 'overcast clouds' or 'broken clouds' 这样的结构,这里它并没有按照您的想法去做。另一方面,这是一个有效的结构,做你期望的事情:first_hour_d == 'overcast clouds' or first_hour_d == 'broken clouds'
  • 不怎么读书,我想你需要:elif a == "a" or a == "aaaa"

标签: python django jinja2


【解决方案1】:

你不能这样做:

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

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

你必须这样做:

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

【讨论】:

  • 谢谢你的工作
猜你喜欢
  • 2021-10-27
  • 2016-01-01
  • 1970-01-01
  • 2019-03-15
  • 2021-06-30
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 2017-12-11
相关资源
最近更新 更多