Django 异常

Django 提供了一些 Django 特定的异常以及许多 Python 异常。

Django 特定异常

ObjectDoesNotExist 和 DoesNotExist

exception DoesNotExist
exception ObjectDoesNotExist

DoesNotExist 当一个给定参数的查询未能发现对象时抛出该异常。

ObjectDoesNotExist 定义在 django.core.exceptions 中。 DoesNotExist 是基类 ObjectDoesNotExist 异常的子类, 用于提供对每一个模型类作为一种识别对象未能发现的特定类型的一种方式。

请查看 get() 获取 ObjectDoesNotExistDoesNotExist 的更多信息。

MultipleObjectsReturned

exception MultipleObjectsReturned

MultipleObjectsReturned 当预期仅返回一个对象的查询确返回多个是抛出的异常。 该异常的基础版本由 django.core.exceptions 提供;每个模型类都 包含一个子类版本用于识别返回多个对象的特定对象类型。

请查看 get() 获取更多信息。

SuspiciousOperation

exception SuspiciousOperation

SuspiciousOperation 当用户执行的操作,从安全的角度来看被视为可疑时抛出的异常,例如篡改一个会话 cookie 。

PermissionDenied

exception PermissionDenied

PermissionDenied 当用户没有权限来执行所请求的操作时抛出的异常。

ViewDoesNotExist

exception ViewDoesNotExist

ViewDoesNotExist 当一个请求的视图不存在时由 django.core.urlresolvers 抛出的异常。

MiddlewareNotUsed

exception MiddlewareNotUsed

MiddlewareNotUsed 当一个中间件在当前服务器配置中不能使用时抛出的异常。

ImproperlyConfigured

exception ImproperlyConfigured

ImproperlyConfigured 当 Django 的某种配置错误时抛出的异常 – 例如 settings.py 中的值错误或不可解析。

FieldError

exception FieldError

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 ValidationError exception is raised when data fails form or model field validation. For more information about validation, see Form and Field Validation, Model Field Validation and the Validator Reference.

NoReverseMatch

exception NoReverseMatch

The NoReverseMatch exception is raised by django.core.urlresolvers when a matching URL in your URLconf cannot be identified based on the parameters supplied.

Database Exceptions

Django wraps the standard database exceptions DatabaseError and IntegrityError so that your Django code has a guaranteed common implementation of these classes. These database exceptions are provided in 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 django.db.models.PROTECT. Subclass of IntegrityError.

Http Exceptions

exception UnreadablePostError

The UnreadablePostError is raised when a user cancels an upload. It is available from django.http.

Transaction Exceptions

exception TransactionManagementError

The TransactionManagementError is raised for any and all problems related to database transactions. It is available from 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 exceptions.