exceptions.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. =================
  2. Django Exceptions
  3. =================
  4. Django raises some Django specific exceptions as well as many standard
  5. Python exceptions.
  6. Django Core Exceptions
  7. ======================
  8. .. module:: django.core.exceptions
  9. :synopsis: Django core exceptions
  10. Django core exception classes are defined in :mod:`django.core.exceptions`.
  11. ObjectDoesNotExist and DoesNotExist
  12. -----------------------------------
  13. .. exception:: DoesNotExist
  14. The ``DoesNotExist`` exception is raised when an object is not found for
  15. the given parameters of a query. Django provides a ``DoesNotExist``
  16. exception as an attribute of each model class to identify the class of
  17. object that could not be found and to allow you to catch a particular model
  18. class with ``try/except``.
  19. .. exception:: ObjectDoesNotExist
  20. The base class for ``DoesNotExist`` exceptions; a ``try/except`` for
  21. ``ObjectDoesNotExist`` will catch ``DoesNotExist`` exceptions for all
  22. models.
  23. See :meth:`~django.db.models.query.QuerySet.get()` for further information
  24. on :exc:`ObjectDoesNotExist` and :exc:`DoesNotExist`.
  25. MultipleObjectsReturned
  26. -----------------------
  27. .. exception:: MultipleObjectsReturned
  28. The :exc:`MultipleObjectsReturned` exception is raised by a query if only
  29. one object is expected, but multiple objects are returned. A base version
  30. of this exception is provided in :mod:`django.core.exceptions`; each model
  31. class contains a subclassed version that can be used to identify the
  32. specific object type that has returned multiple objects.
  33. See :meth:`~django.db.models.query.QuerySet.get()` for further information.
  34. SuspiciousOperation
  35. -------------------
  36. .. exception:: SuspiciousOperation
  37. The :exc:`SuspiciousOperation` exception is raised when a user has
  38. performed an operation that should be considered suspicious from a security
  39. perspective, such as tampering with a session cookie. Subclasses of
  40. SuspiciousOperation include:
  41. * DisallowedHost
  42. * DisallowedModelAdminLookup
  43. * DisallowedRedirect
  44. * InvalidSessionKey
  45. * SuspiciousFileOperation
  46. * SuspiciousMultipartForm
  47. * SuspiciousSession
  48. * WizardViewCookieModified
  49. If a ``SuspiciousOperation`` exception reaches the WSGI handler level it is
  50. logged at the ``Error`` level and results in
  51. a :class:`~django.http.HttpResponseBadRequest`. See the :doc:`logging
  52. documentation </topics/logging/>` for more information.
  53. PermissionDenied
  54. ----------------
  55. .. exception:: PermissionDenied
  56. The :exc:`PermissionDenied` exception is raised when a user does not have
  57. permission to perform the action requested.
  58. ViewDoesNotExist
  59. ----------------
  60. .. exception:: ViewDoesNotExist
  61. The :exc:`ViewDoesNotExist` exception is raised by
  62. :mod:`django.core.urlresolvers` when a requested view does not exist.
  63. MiddlewareNotUsed
  64. -----------------
  65. .. exception:: MiddlewareNotUsed
  66. The :exc:`MiddlewareNotUsed` exception is raised when a middleware is not
  67. used in the server configuration.
  68. ImproperlyConfigured
  69. --------------------
  70. .. exception:: ImproperlyConfigured
  71. The :exc:`ImproperlyConfigured` exception is raised when Django is
  72. somehow improperly configured -- for example, if a value in ``settings.py``
  73. is incorrect or unparseable.
  74. FieldError
  75. ----------
  76. .. exception:: FieldError
  77. The :exc:`FieldError` exception is raised when there is a problem with a
  78. model field. This can happen for several reasons:
  79. - A field in a model clashes with a field of the same name from an
  80. abstract base class
  81. - An infinite loop is caused by ordering
  82. - A keyword cannot be parsed from the filter parameters
  83. - A field cannot be determined from a keyword in the query
  84. parameters
  85. - A join is not permitted on the specified field
  86. - A field name is invalid
  87. - A query contains invalid order_by arguments
  88. ValidationError
  89. ---------------
  90. .. exception:: ValidationError
  91. The :exc:`ValidationError` exception is raised when data fails form or
  92. model field validation. For more information about validation, see
  93. :doc:`Form and Field Validation </ref/forms/validation>`,
  94. :ref:`Model Field Validation <validating-objects>` and the
  95. :doc:`Validator Reference </ref/validators>`.
  96. NON_FIELD_ERRORS
  97. ~~~~~~~~~~~~~~~~
  98. .. data:: NON_FIELD_ERRORS
  99. ``ValidationError``\s that don't belong to a particular field in a form
  100. or model are classified as ``NON_FIELD_ERRORS``. This constant is used
  101. as a key in dictionaries that otherwise map fields to their respective
  102. list of errors.
  103. .. currentmodule:: django.core.urlresolvers
  104. URL Resolver exceptions
  105. =======================
  106. URL Resolver exceptions are defined in :mod:`django.core.urlresolvers`.
  107. Resolver404
  108. --------------
  109. .. exception:: Resolver404
  110. The :exc:`Resolver404` exception is raised by
  111. :func:`django.core.urlresolvers.resolve()` if the path passed to
  112. ``resolve()`` doesn't map to a view. It's a subclass of
  113. :class:`django.http.Http404`
  114. NoReverseMatch
  115. --------------
  116. .. exception:: NoReverseMatch
  117. The :exc:`NoReverseMatch` exception is raised by
  118. :mod:`django.core.urlresolvers` when a matching URL in your URLconf
  119. cannot be identified based on the parameters supplied.
  120. .. currentmodule:: django.db
  121. Database Exceptions
  122. ===================
  123. Database exceptions are provided in :mod:`django.db`.
  124. Django wraps the standard database exceptions so that your Django code has a
  125. guaranteed common implementation of these classes.
  126. .. exception:: Error
  127. .. exception:: InterfaceError
  128. .. exception:: DatabaseError
  129. .. exception:: DataError
  130. .. exception:: OperationalError
  131. .. exception:: IntegrityError
  132. .. exception:: InternalError
  133. .. exception:: ProgrammingError
  134. .. exception:: NotSupportedError
  135. The Django wrappers for database exceptions behave exactly the same as
  136. the underlying database exceptions. See :pep:`249`, the Python Database API
  137. Specification v2.0, for further information.
  138. As per :pep:`3134`, a ``__cause__`` attribute is set with the original
  139. (underlying) database exception, allowing access to any additional
  140. information provided. (Note that this attribute is available under
  141. both Python 2 and Python 3, although :pep:`3134` normally only applies
  142. to Python 3.)
  143. .. versionchanged:: 1.6
  144. Previous versions of Django only wrapped ``DatabaseError`` and
  145. ``IntegrityError``, and did not provide ``__cause__``.
  146. .. exception:: models.ProtectedError
  147. Raised to prevent deletion of referenced objects when using
  148. :attr:`django.db.models.PROTECT`. :exc:`models.ProtectedError` is a subclass
  149. of :exc:`IntegrityError`.
  150. .. currentmodule:: django.http
  151. Http Exceptions
  152. ===============
  153. Http exceptions are provided in :mod:`django.http`.
  154. .. exception:: UnreadablePostError
  155. The :exc:`UnreadablePostError` is raised when a user cancels an upload.
  156. .. currentmodule:: django.db.transaction
  157. Transaction Exceptions
  158. ======================
  159. Transaction exceptions are defined in :mod:`django.db.transaction`.
  160. .. exception:: TransactionManagementError
  161. The :exc:`TransactionManagementError` is raised for any and all problems
  162. related to database transactions.
  163. Python Exceptions
  164. =================
  165. Django raises built-in Python exceptions when appropriate as well. See the
  166. Python documentation for further information on the
  167. built-in :mod:`exceptions`.