exceptions.txt 7.8 KB

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