如何在 Django 中使用 try 和 except ?

How to use try and except in django?

我想在 djagno 模板中使用 try 和 except 有什么方法可以使用它。

我想查考勤table有没有考勤的数据。如果不是,它必须显示错误文本

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

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

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

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