【问题标题】:Display a message if loop is empty如果循环为空,则显示一条消息
【发布时间】:2019-02-26 08:55:51
【问题描述】:

我想在我的模板中使用“if”来显示“无内容”之类的消息。

我试过了:

views.py

 {% for links in links_list %} 
    {% if links is None %}

    <p>No content</p>

     {% else %}

    <p>some content</p>
{% endif %}{% endfor %}

它不起作用,谢谢。

【问题讨论】:

    标签: python django if-statement django-templates


    【解决方案1】:

    您可以为此使用标签for-empty

    {% empty %}
    

    例如:

    {% for links in links_list %} 
     <p>some content</p>
      {% empty %}
       <p>No content</p>
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多