python - 捕获任何 DoesNotExist 错误

标签 python django django-errors

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

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

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

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

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

最佳答案

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

from django.core.exceptions import ObjectDoesNotExist

try:
    # ...
except ObjectDoesNotExist as den:
    # handle exception

关于python - 捕获任何 DoesNotExist 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25928990/

相关文章:

python - 如果还有其他输入意味着出了点问题,我应该如何结束if-else block ?

django - collecstatic 不会推送到文件 S3

django - 函数内的 Postgres 咨询锁允许并发执行

python - 奇怪的 django 导入错误

python - Windows 上 Python 3.1 的 SendKeys

python - 请求.exceptions.SSLError : Errno 185090050

python - 递归函数的危险

django - Docker + Gunicorn + Nginx + Django : redirect non-www to www on AWS Route 53

django - 错误 : No module named messages after installing python-django-debug-toolbar

django - ModelChoiceField 上的验证错误