【问题标题】:How to use try and except in django?如何在 django 中使用 try 和 except?
【发布时间】:2021-08-31 17:16:31
【问题描述】:

我想使用try,除了在djagno模板中是否有任何使用方法。

我想检查任一考勤表是否有任何数据。如果不是,它必须显示错误文本

{% try %}
    {% for attendances in attendance %}
        ....
    {% endfor %}
{% except %}
    <h1>Error Attendance table has no data.</h1>

【问题讨论】:

    标签: django django-models django-views django-templates


    【解决方案1】:

    你可以使用 Django 内置的 for..empty 模板标签来代替 try-catch。

    {% for attendances in attendance %}
            ....
    {% empty %}
        <h1>Error Attendance table has no data.</h1>
    {% endfor %}
    

    如果表为空,这将显示错误消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多