================= Django 异常 ================= Django 提供了一些 Django 特定的异常以及许多 Python 异常。 Django 特定异常 ========================== .. module:: django.core.exceptions :synopsis: Django specific exceptions ObjectDoesNotExist 和 DoesNotExist ----------------------------------- .. exception:: DoesNotExist .. exception:: ObjectDoesNotExist :exc:`DoesNotExist` 当一个给定参数的查询未能发现对象时抛出该异常。 :exc:`ObjectDoesNotExist` 定义在 :mod:`django.core.exceptions` 中。 :exc:`DoesNotExist` 是基类 :exc:`ObjectDoesNotExist` 异常的子类, 用于提供对每一个模型类作为一种识别对象未能发现的特定类型的一种方式。 请查看 :meth:`~django.db.models.query.QuerySet.get()` 获取 :exc:`ObjectDoesNotExist` 和 :exc:`DoesNotExist` 的更多信息。 MultipleObjectsReturned ----------------------- .. exception:: MultipleObjectsReturned :exc:`MultipleObjectsReturned` 当预期仅返回一个对象的查询确返回多个是抛出的异常。 该异常的基础版本由 :mod:`django.core.exceptions` 提供;每个模型类都 包含一个子类版本用于识别返回多个对象的特定对象类型。 请查看 :meth:`~django.db.models.query.QuerySet.get()` 获取更多信息。 SuspiciousOperation ------------------- .. exception:: SuspiciousOperation :exc:`SuspiciousOperation` 当用户执行的操作,从安全的角度来看被视为可疑时抛出的异常,例如篡改一个会话 cookie 。 PermissionDenied ---------------- .. exception:: PermissionDenied :exc:`PermissionDenied` 当用户没有权限来执行所请求的操作时抛出的异常。 ViewDoesNotExist ---------------- .. exception:: ViewDoesNotExist :exc:`ViewDoesNotExist` 当一个请求的视图不存在时由 :mod:`django.core.urlresolvers` 抛出的异常。 MiddlewareNotUsed ----------------- .. exception:: MiddlewareNotUsed :exc:`MiddlewareNotUsed` 当一个中间件在当前服务器配置中不能使用时抛出的异常。 ImproperlyConfigured -------------------- .. exception:: ImproperlyConfigured :exc:`ImproperlyConfigured` 当 Django 的某种配置错误时抛出的异常 -- 例如 ``settings.py`` 中的值错误或不可解析。 FieldError ---------- .. exception:: FieldError :exc:`FieldError` 当一个模型字段有问题是抛出的异常。 发生这种情况的原因有几个: - 模型类中的字段名与抽象基类中的字段名相同而发生冲突 - 一个由排序造成的无限循环 - 一个关键字不能从过滤参数解析 - A field cannot be determined from a keyword in the query parameters - A join is not permitted on the specified field - 字段名无效 - 一个查询包含无效排序参数 ValidationError --------------- .. exception:: ValidationError The :exc:`ValidationError` exception is raised when data fails form or model field validation. For more information about validation, see :doc:`Form and Field Validation `, :ref:`Model Field Validation ` and the :doc:`Validator Reference `. .. currentmodule:: django.core.urlresolvers NoReverseMatch -------------- .. exception:: NoReverseMatch The :exc:`NoReverseMatch` exception is raised by :mod:`django.core.urlresolvers` when a matching URL in your URLconf cannot be identified based on the parameters supplied. .. currentmodule:: django.db Database Exceptions =================== Django wraps the standard database exceptions :exc:`DatabaseError` and :exc:`IntegrityError` so that your Django code has a guaranteed common implementation of these classes. These database exceptions are provided in :mod:`django.db`. .. exception:: DatabaseError .. exception:: IntegrityError The Django wrappers for database exceptions behave exactly the same as the underlying database exceptions. See :pep:`249`, the Python Database API Specification v2.0, for further information. .. exception:: models.ProtectedError Raised to prevent deletion of referenced objects when using :attr:`django.db.models.PROTECT`. Subclass of :exc:`IntegrityError`. .. currentmodule:: django.http Http Exceptions =============== .. exception:: UnreadablePostError The :exc:`UnreadablePostError` is raised when a user cancels an upload. It is available from :mod:`django.http`. .. currentmodule:: django.db.transaction Transaction Exceptions ====================== .. exception:: TransactionManagementError The :exc:`TransactionManagementError` is raised for any and all problems related to database transactions. It is available from :mod:`django.db.transaction`. Python Exceptions ================= Django raises built-in Python exceptions when appropriate as well. See the Python documentation for further information on the built-in :mod:`exceptions`.