exceptions.txt 8.1 KB

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