【问题标题】:Catching Any DoesNotExist Error捕获任何 DoesNotExist 错误
【发布时间】:2014-09-19 07:55:11
【问题描述】:

我正在使用 Django 1.7。通常你可以在你的模型上捕获DoesNotExist 异常;

try:
   ...
except model.DoesNotExist, den:
   ...

我想捕捉任何DoesNotExist 异常。我真的不想关心它的模型。实际上,我也真的不知道 DoesNotExist 正在通过代码片段的哪个模型。我的意思是,我不知道模型。

所以我必须以某种方式捕获任何DoesNotExist 错误。

有没有办法捕获Django 中的任何DoesNotExist 错误?

【问题讨论】:

    标签: python django django-errors


    【解决方案1】:

    DoesNotExist 异常是django.core.exceptions.ObjectDoesNotExist 的子类:

    from django.core.exceptions import ObjectDoesNotExist
    
    try:
        # ...
    except ObjectDoesNotExist as den:
        # handle exception
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-12
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      相关资源
      最近更新 更多